Instruction:
Write a Python 3 function that can
create a network which represents an L2 network segment which can be used to create a set of subnets and ports associated with it.

Response:
def create_network(context, network):
    """Create a network.

    Create a network which represents an L2 network segment which
    can have a set of subnets and ports associated with it.
    : param context: neutron api request context
    : param network: dictionary describing the network, with keys
        as listed in the RESOURCE_ATTRIBUTE_MAP object in
        neutron/api/v2/attributes.py.  All keys will be populated.
    """
    LOG.info("create_network for tenant %s" % context.tenant_id)

    with context.session.begin():
        net_attrs = network["network"]
        subs = net_attrs.pop("subnets", [])
        # Enforce subnet quotas
        if not context.is_admin:
            if len(subs) > 0:
                v4_count, v6_count = 0, 0
                for s in subs:
                    version = netaddr.IPNetwork(s['subnet']['cidr']).version
                    if version == 6:
                        v6_count += 1
                    else:
                        v4_count += 1
                if v4_count > 0:
                    tenant_q_v4 = context.session.query(qdv.Quota).filter_by(
                        tenant_id=context.tenant_id,
                        resource='v4_subnets_per_network').first()
                    if tenant_q_v4 != -1:
                        quota.QUOTAS.limit_check(
                            context,
                            context.tenant_id,
                            v4_subnets_per_network=v4_count)
                if v6_count > 0:
                    tenant_q_v6 = context.session.query(qdv.Quota).filter_by(
                        tenant_id=context.tenant_id,
                        resource='v6_subnets_per_network').first()
                    if tenant_q_v6 != -1:
                        quota.QUOTAS.limit_check(
                            context,
                            context.tenant_id,
                            v6_subnets_per_network=v6_count)
        # Generate a uuid that we're going to hand to the backend and db
        net_uuid = utils.pop_param(net_attrs, "id", None)
        net_type = None
        if net_uuid and context.is_admin:
            net = db_api.network_find(context=context, limit=None,
                                      sorts=['id'], marker=None,
                                      page_reverse=False, id=net_uuid,
                                      scope=db_api.ONE)
            net_type = utils.pop_param(net_attrs, "network_plugin", None)
            if net:
                raise q_exc.NetworkAlreadyExists(id=net_uuid)
        else:
            net_uuid = uuidutils.generate_uuid()

        # TODO(mdietz) this will be the first component registry hook, but
        #             lets make it work first
        pnet_type, phys_net, seg_id = _adapt_provider_nets(context, network)

        ipam_strategy = utils.pop_param(net_attrs, "ipam_strategy", None)
        if not ipam_strategy or not context.is_admin:
            ipam_strategy = CONF.QUARK.default_ipam_strategy

        if not ipam.IPAM_REGISTRY.is_valid_strategy(ipam_strategy):
            raise q_exc.InvalidIpamStrategy(strat=ipam_strategy)
        net_attrs["ipam_strategy"] = ipam_strategy

        # NOTE(mdietz) I think ideally we would create the providernet
        # elsewhere as a separate driver step that could be
        # kept in a plugin and completely removed if desired. We could
        # have a pre-callback/observer on the netdriver create_network
        # that gathers any additional parameters from the network dict

        default_net_type = net_type or CONF.QUARK.default_network_type
        net_driver = registry.DRIVER_REGISTRY.get_driver(default_net_type)
        net_driver.create_network(context, net_attrs["name"],
                                  network_id=net_uuid, phys_type=pnet_type,
                                  phys_net=phys_net, segment_id=seg_id)

        net_attrs["id"] = net_uuid
        net_attrs["tenant_id"] = context.tenant_id
        net_attrs["network_plugin"] = default_net_type
        new_net = db_api.network_create(context, **net_attrs)

        new_subnets = []
        for sub in subs:
            sub["subnet"]["network_id"] = new_net["id"]
            sub["subnet"]["tenant_id"] = context.tenant_id
            s = db_api.subnet_create(context, **sub["subnet"])
            new_subnets.append(s)
        new_net["subnets"] = new_subnets

        # if not security_groups.get_security_groups(
        #        context,
        #        filters={"id": security_groups.DEFAULT_SG_UUID}):
        #    security_groups._create_default_security_group(context)
    return v._make_network_dict(new_net)


Instruction:
Can you create a Python 3 function that
updates the values of a network.

Response:
def update_network(context, id, network):
    """Update values of a network.

    : param context: neutron api request context
    : param id: UUID representing the network to update.
    : param network: dictionary with keys indicating fields to update.
        valid keys are those that have a value of True for 'allow_put'
        as listed in the RESOURCE_ATTRIBUTE_MAP object in
        neutron/api/v2/attributes.py.
    """
    LOG.info("update_network %s for tenant %s" %
             (id, context.tenant_id))
    with context.session.begin():
        net = db_api.network_find(context, id=id, scope=db_api.ONE)
        if not net:
            raise n_exc.NetworkNotFound(net_id=id)
        net_dict = network["network"]
        utils.pop_param(net_dict, "network_plugin")
        if not context.is_admin and "ipam_strategy" in net_dict:
            utils.pop_param(net_dict, "ipam_strategy")
        net = db_api.network_update(context, net, **net_dict)

    return v._make_network_dict(net)


Instruction:
Can you write a function in Python 3 where it
retrieves a network. : param context: neutron api request context : param id: UUID representing the network to fetch. : param fields: a list of strings that are valid keys in a network dictionary as listed in the RESOURCE_ATTRIBUTE_MAP object in neutron/api/v2/attributes.py. Only these fields will be returned.

Response:
def get_network(context, id, fields=None):
    """Retrieve a network.

    : param context: neutron api request context
    : param id: UUID representing the network to fetch.
    : param fields: a list of strings that are valid keys in a
        network dictionary as listed in the RESOURCE_ATTRIBUTE_MAP
        object in neutron/api/v2/attributes.py. Only these fields
        will be returned.
    """
    LOG.info("get_network %s for tenant %s fields %s" %
             (id, context.tenant_id, fields))

    network = db_api.network_find(context=context, limit=None, sorts=['id'],
                                  marker=None, page_reverse=False,
                                  id=id, join_subnets=True, scope=db_api.ONE)
    if not network:
        raise n_exc.NetworkNotFound(net_id=id)
    return v._make_network_dict(network, fields=fields)


Instruction:
Write a Python 3 function that can
retrieve a list of networks for the user in the tenant.

Response:
def get_networks(context, limit=None, sorts=['id'], marker=None,
                 page_reverse=False, filters=None, fields=None):
    """Retrieve a list of networks.

    The contents of the list depends on the identity of the user
    making the request (as indicated by the context) as well as any
    filters.
    : param context: neutron api request context
    : param filters: a dictionary with keys that are valid keys for
        a network as listed in the RESOURCE_ATTRIBUTE_MAP object
        in neutron/api/v2/attributes.py.  Values in this dictiontary
        are an iterable containing values that will be used for an exact
        match comparison for that value.  Each result returned by this
        function will have matched one of the values for each key in
        filters.
    : param fields: a list of strings that are valid keys in a
        network dictionary as listed in the RESOURCE_ATTRIBUTE_MAP
        object in neutron/api/v2/attributes.py. Only these fields
        will be returned.
    """
    LOG.info("get_networks for tenant %s with filters %s, fields %s" %
             (context.tenant_id, filters, fields))
    filters = filters or {}
    nets = db_api.network_find(context, limit, sorts, marker, page_reverse,
                               join_subnets=True, **filters) or []
    nets = [v._make_network_dict(net, fields=fields) for net in nets]
    return nets


Instruction:
Here you have a function in Python 3, explain what it does
def get_networks_count(context, filters=None):
    LOG.info("get_networks_count for tenant %s filters %s" %
             (context.tenant_id, filters))
    return db_api.network_count_all(context)

Response: Return the number of networks in the current tenant.


Instruction:
Write a Python 3 function for
deleting a network. : param context: neutron api request context : param id: UUID representing the network to delete.

Response:
def delete_network(context, id):
    """Delete a network.

    : param context: neutron api request context
    : param id: UUID representing the network to delete.
    """
    LOG.info("delete_network %s for tenant %s" % (id, context.tenant_id))
    with context.session.begin():
        net = db_api.network_find(context=context, limit=None, sorts=['id'],
                                  marker=None, page_reverse=False, id=id,
                                  scope=db_api.ONE)
        if not net:
            raise n_exc.NetworkNotFound(net_id=id)
        if not context.is_admin:
            if STRATEGY.is_provider_network(net.id):
                raise n_exc.NotAuthorized(net_id=id)
        if net.ports:
            raise n_exc.NetworkInUse(net_id=id)
        net_driver = registry.DRIVER_REGISTRY.get_driver(net["network_plugin"])
        net_driver.delete_network(context, id)
        for subnet in net["subnets"]:
            subnets._delete_subnet(context, subnet)
        db_api.network_delete(context, net)


Instruction:
Given the following Python 3 function, write the documentation
def make_case2(context):
    query = context.session.query(models.IPAddress)
    period_start, period_end = billing.calc_periods()
    ip_list = billing.build_full_day_ips(query, period_start, period_end)
    import random
    ind = random.randint(0, len(ip_list) - 1)
    address = ip_list[ind]
    address.allocated_at = datetime.datetime.utcnow() -\
        datetime.timedelta(days=1)
    context.session.add(address)
    context.session.flush()

Response: This method is a helper method for testing. It will create a case 2 entry in the database.


Instruction:
Can you write a function in Python 3 where it
runs the billing report. Optionally sends notifications to billing

Response:
def main(notify, hour, minute):
    """Runs billing report. Optionally sends notifications to billing"""

    # Read the config file and get the admin context
    config_opts = ['--config-file', '/etc/neutron/neutron.conf']
    config.init(config_opts)
    # Have to load the billing module _after_ config is parsed so
    # that we get the right network strategy
    network_strategy.STRATEGY.load()
    billing.PUBLIC_NETWORK_ID = network_strategy.STRATEGY.get_public_net_id()
    config.setup_logging()
    context = neutron_context.get_admin_context()

    # A query to get all IPAddress objects from the db
    query = context.session.query(models.IPAddress)

    (period_start, period_end) = billing.calc_periods(hour, minute)

    full_day_ips = billing.build_full_day_ips(query,
                                              period_start,
                                              period_end)
    partial_day_ips = billing.build_partial_day_ips(query,
                                                    period_start,
                                                    period_end)

    if notify:
        # '==================== Full Day ============================='
        for ipaddress in full_day_ips:
            click.echo('start: {}, end: {}'.format(period_start, period_end))
            payload = billing.build_payload(ipaddress,
                                            billing.IP_EXISTS,
                                            start_time=period_start,
                                            end_time=period_end)
            billing.do_notify(context,
                              billing.IP_EXISTS,
                              payload)
        # '==================== Part Day ============================='
        for ipaddress in partial_day_ips:
            click.echo('start: {}, end: {}'.format(period_start, period_end))
            payload = billing.build_payload(ipaddress,
                                            billing.IP_EXISTS,
                                            start_time=ipaddress.allocated_at,
                                            end_time=period_end)
            billing.do_notify(context,
                              billing.IP_EXISTS,
                              payload)
    else:
        click.echo('Case 1 ({}):\n'.format(len(full_day_ips)))
        for ipaddress in full_day_ips:
            pp(billing.build_payload(ipaddress,
                                     billing.IP_EXISTS,
                                     start_time=period_start,
                                     end_time=period_end))

        click.echo('\n===============================================\n')

        click.echo('Case 2 ({}):\n'.format(len(partial_day_ips)))
        for ipaddress in partial_day_ips:
            pp(billing.build_payload(ipaddress,
                                     billing.IP_EXISTS,
                                     start_time=ipaddress.allocated_at,
                                     end_time=period_end))


Instruction:
Can you write a function in Python 3 where it
configures all listeners here

Response:
def start_rpc_listeners(self):
        """Configure all listeners here"""
        self._setup_rpc()
        if not self.endpoints:
            return []
        self.conn = n_rpc.create_connection()
        self.conn.create_consumer(self.topic, self.endpoints,
                                  fanout=False)
        return self.conn.consume_in_threads()


Instruction:
Can you generate the documentation for the following Python 3 function
def context(self):
        if not self._context:
            self._context = context.get_admin_context()
        return self._context

Response: Provides an admin context for workers.


Instruction:
Create a Python 3 function for
starting the async update process.

Response:
def update_sg(self, context, sg, rule_id, action):
        """Begins the async update process."""
        db_sg = db_api.security_group_find(context, id=sg, scope=db_api.ONE)
        if not db_sg:
            return None
        with context.session.begin():
            job_body = dict(action="%s sg rule %s" % (action, rule_id),
                            resource_id=rule_id,
                            tenant_id=db_sg['tenant_id'])
            job_body = dict(job=job_body)
            job = job_api.create_job(context.elevated(), job_body)
            rpc_client = QuarkSGAsyncProducerClient()
            try:
                rpc_client.populate_subtasks(context, sg, job['id'])
            except om_exc.MessagingTimeout:
                LOG.error("Failed to create subtasks. Rabbit running?")
                return None
        return {"job_id": job['id']}


Instruction:
Explain what the following Python 3 code does
def populate_subtasks(self, context, sg, parent_job_id):
        db_sg = db_api.security_group_find(context, id=sg, scope=db_api.ONE)
        if not db_sg:
            return None
        ports = db_api.sg_gather_associated_ports(context, db_sg)
        if len(ports) == 0:
            return {"ports": 0}
        for port in ports:
            job_body = dict(action="update port %s" % port['id'],
                            tenant_id=db_sg['tenant_id'],
                            resource_id=port['id'],
                            parent_id=parent_job_id)
            job_body = dict(job=job_body)
            job = job_api.create_job(context.elevated(), job_body)
            rpc_consumer = QuarkSGAsyncConsumerClient()
            try:
                rpc_consumer.update_port(context, port['id'], job['id'])
            except om_exc.MessagingTimeout:
                # TODO(roaet): Not too sure what can be done here other than
                # updating the job as a failure?
                LOG.error("Failed to update port. Rabbit running?")
        return None

Response: Populate the list of ports to be updated async.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def update_ports_for_sg(self, context, portid, jobid):
        port = db_api.port_find(context, id=portid, scope=db_api.ONE)
        if not port:
            LOG.warning("Port not found")
            return
        net_driver = port_api._get_net_driver(port.network, port=port)
        base_net_driver = port_api._get_net_driver(port.network)
        sg_list = [sg for sg in port.security_groups]

        success = False
        error = None
        retries = 3
        retry_delay = 2
        for retry in xrange(retries):
            try:
                net_driver.update_port(context, port_id=port["backend_key"],
                                       mac_address=port["mac_address"],
                                       device_id=port["device_id"],
                                       base_net_driver=base_net_driver,
                                       security_groups=sg_list)
                success = True
                error = None
                break
            except Exception as error:
                LOG.warning("Could not connect to redis, but retrying soon")
                time.sleep(retry_delay)
        status_str = ""
        if not success:
            status_str = "Port %s update failed after %d tries. Error: %s" % (
                portid, retries, error)
        update_body = dict(completed=True, status=status_str)
        update_body = dict(job=update_body)
        job_api.update_job(context.elevated(), jobid, update_body)

Response: Updates the ports through redis.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def sg_gather_associated_ports(context, group):
    if not group:
        return None
    if not hasattr(group, "ports") or len(group.ports) <= 0:
        return []
    return group.ports

Response: Gather all ports associated to security group.


Instruction:
Write a Python 3 script to
update a security group rule.

Response:
def security_group_rule_update(context, rule, **kwargs):
    '''Updates a security group rule.

    NOTE(alexm) this is non-standard functionality.
    '''
    rule.update(kwargs)
    context.session.add(rule)
    return rule


Instruction:
Create a Python 3 function for
querying for segment allocations.

Response:
def segment_allocation_find(context, lock_mode=False, **filters):
    """Query for segment allocations."""
    range_ids = filters.pop("segment_allocation_range_ids", None)

    query = context.session.query(models.SegmentAllocation)
    if lock_mode:
        query = query.with_lockmode("update")

    query = query.filter_by(**filters)

    # Optionally filter by given list of range ids
    if range_ids:
        query.filter(
            models.SegmentAllocation.segment_allocation_range_id.in_(
                range_ids))
    return query


Instruction:
Write a Python 3 function that can
send a command to Niko Home Control and returns the output of the system.

Response:
def send(self, s):
        """
        Sends the given command to Niko Home Control and returns the output of
        the system.

        Aliases: write, put, sendall, send_all
        """
        self._socket.send(s.encode())
        return self.read()


Instruction:
Make a summary of the following Python 3 code
def if_(*args):
    for i in range(0, len(args) - 1, 2):
        if args[i]:
            return args[i + 1]
    if len(args) % 2:
        return args[-1]
    else:
        return None

Response: Implements the if operator with support for multiple elseif - s.


Instruction:
How would you explain what the following Python 3 function does
def soft_equals(a, b):
    if isinstance(a, str) or isinstance(b, str):
        return str(a) == str(b)
    if isinstance(a, bool) or isinstance(b, bool):
        return bool(a) is bool(b)
    return a == b

Response: Implements the '==' operator which does type JS - style coertion.


Instruction:
Can you implement a function in Python 3 that
implements the == operator.

Response:
def hard_equals(a, b):
    """Implements the '===' operator."""
    if type(a) != type(b):
        return False
    return a == b


Instruction:
How would you implement a function in Python 3 that
implements the '<' operator with JS - style type coertion.

Response:
def less(a, b, *args):
    """Implements the '<' operator with JS-style type coertion."""
    types = set([type(a), type(b)])
    if float in types or int in types:
        try:
            a, b = float(a), float(b)
        except TypeError:
            # NaN
            return False
    return a < b and (not args or less(b, *args))


Instruction:
Make a summary of the following Python 3 code
def less_or_equal(a, b, *args):
    return (
        less(a, b) or soft_equals(a, b)
    ) and (not args or less_or_equal(b, *args))

Response: Implements the '<=' operator with JS - style type coertion.


Instruction:
Can you implement a function in Python 3 that
converts a string either to int float or a string.

Response:
def to_numeric(arg):
    """
    Converts a string either to int or to float.
    This is important, because e.g. {"!==": [{"+": "0"}, 0.0]}
    """
    if isinstance(arg, str):
        if '.' in arg:
            return float(arg)
        else:
            return int(arg)
    return arg


Instruction:
How would you code a function in Python 3 to
implement the merge operator for merging lists.

Response:
def merge(*args):
    """Implements the 'merge' operator for merging lists."""
    ret = []
    for arg in args:
        if isinstance(arg, list) or isinstance(arg, tuple):
            ret += list(arg)
        else:
            ret.append(arg)
    return ret


Instruction:
Write a Python 3 script for
getting variable value from data dictionary.

Response:
def get_var(data, var_name, not_found=None):
    """Gets variable value from data dictionary."""
    try:
        for key in str(var_name).split('.'):
            try:
                data = data[key]
            except TypeError:
                data = data[int(key)]
    except (KeyError, TypeError, ValueError):
        return not_found
    else:
        return data


Instruction:
Make a summary of the following Python 3 code
def missing(data, *args):
    not_found = object()
    if args and isinstance(args[0], list):
        args = args[0]
    ret = []
    for arg in args:
        if get_var(data, arg, not_found) is not_found:
            ret.append(arg)
    return ret

Response: Implements the missing operator for finding missing variables.


Instruction:
Create a Python 3 function to
implement the missing_some operator for finding missing variables.

Response:
def missing_some(data, min_required, args):
    """Implements the missing_some operator for finding missing variables."""
    if min_required < 1:
        return []
    found = 0
    not_found = object()
    ret = []
    for arg in args:
        if get_var(data, arg, not_found) is not_found:
            ret.append(arg)
        else:
            found += 1
            if found >= min_required:
                return []
    return ret


Instruction:
Can you create a Python 3 function that
executes the json - logic with given data.

Response:
def jsonLogic(tests, data=None):
    """Executes the json-logic with given data."""
    # You've recursed to a primitive, stop!
    if tests is None or not isinstance(tests, dict):
        return tests

    data = data or {}

    operator = list(tests.keys())[0]
    values = tests[operator]

    # Easy syntax for unary operators, like {"var": "x"} instead of strict
    # {"var": ["x"]}
    if not isinstance(values, list) and not isinstance(values, tuple):
        values = [values]

    # Recursion!
    values = [jsonLogic(val, data) for val in values]

    if operator == 'var':
        return get_var(data, *values)
    if operator == 'missing':
        return missing(data, *values)
    if operator == 'missing_some':
        return missing_some(data, *values)

    if operator not in operations:
        raise ValueError("Unrecognized operation %s" % operator)

    return operations[operator](*values)


Instruction:
Implement a function in Python 3 to
perform an indentation on the current line of text.

Response:
def indent(self):
        """
        Performs an indentation
        """
        if not self.tab_always_indent:
            super(PyIndenterMode, self).indent()
        else:
            cursor = self.editor.textCursor()
            assert isinstance(cursor, QtGui.QTextCursor)
            if cursor.hasSelection():
                self.indent_selection(cursor)
            else:
                # simply insert indentation at the cursor position
                tab_len = self.editor.tab_length
                cursor.beginEditBlock()
                if self.editor.use_spaces_instead_of_tabs:
                    cursor.insertText(tab_len * " ")
                else:
                    cursor.insertText('\t')
                cursor.endEditBlock()
                self.editor.setTextCursor(cursor)


Instruction:
Create a Python 3 function for
performing an un - indentation on the current item.

Response:
def unindent(self):
        """
        Performs an un-indentation
        """
        if self.tab_always_indent:
            cursor = self.editor.textCursor()
            if not cursor.hasSelection():
                cursor.select(cursor.LineUnderCursor)
            self.unindent_selection(cursor)
        else:
            super(PyIndenterMode, self).unindent()


Instruction:
Can you generate a brief explanation for the following Python 3 code
def _handle_indent_between_paren(self, column, line, parent_impl, tc):
        pre, post = parent_impl
        next_char = self._get_next_char(tc)
        prev_char = self._get_prev_char(tc)
        prev_open = prev_char in ['[', '(', '{']
        next_close = next_char in [']', ')', '}']
        (open_line, open_symbol_col), (close_line, close_col) = \
            self._get_paren_pos(tc, column)
        open_line_txt = self._helper.line_text(open_line)
        open_line_indent = len(open_line_txt) - len(open_line_txt.lstrip())
        if prev_open:
            post = (open_line_indent + self.editor.tab_length) * ' '
        elif next_close and prev_char != ',':
            post = open_line_indent * ' '
        elif tc.block().blockNumber() == open_line:
            post = open_symbol_col * ' '

        # adapt indent if cursor on closing line and next line have same
        # indent -> PEP8 compliance
        if close_line and close_col:
            txt = self._helper.line_text(close_line)
            bn = tc.block().blockNumber()
            flg = bn == close_line
            next_indent = self._helper.line_indent(bn + 1) * ' '
            if flg and txt.strip().endswith(':') and next_indent == post:
                # | look at how the previous line ( ``':'):`` ) was
                # over-indented, this is actually what we are trying to
                # achieve here
                post += self.editor.tab_length * ' '

        # breaking string
        if next_char in ['"', "'"]:
            tc.movePosition(tc.Left)
        is_string = self._helper.is_comment_or_string(tc, formats=['string'])
        if next_char in ['"', "'"]:
            tc.movePosition(tc.Right)
        if is_string:
            trav = QTextCursor(tc)
            while self._helper.is_comment_or_string(
                    trav, formats=['string']):
                trav.movePosition(trav.Left)
            trav.movePosition(trav.Right)
            symbol = '%s' % self._get_next_char(trav)
            pre += symbol
            post += symbol

        return pre, post

Response: Handle indentation between parentheses and braces.


Instruction:
Can you generate the documentation for the following Python 3 function
def _at_block_start(tc, line):
        if tc.atBlockStart():
            return True
        column = tc.columnNumber()
        indentation = len(line) - len(line.lstrip())
        return column <= indentation

Response: Return True if the cursor is at the start of a block.


Instruction:
Given the following Python 3 function, write the documentation
def detect_encoding(self, path):
        with open(path, 'rb') as file:
            source = file.read()
            # take care of line encodings (not in jedi)
            source = source.replace(b'\r', b'')
            source_str = str(source).replace('\\n', '\n')

        byte_mark = ast.literal_eval(r"b'\xef\xbb\xbf'")
        if source.startswith(byte_mark):
            # UTF-8 byte-order mark
            return 'utf-8'

        first_two_lines = re.match(r'(?:[^\n]*\n){0,2}', source_str).group(0)
        possible_encoding = re.search(r"coding[=:]\s*([-\w.]+)",
                                      first_two_lines)
        if possible_encoding:
            return possible_encoding.group(1)
        return 'UTF-8'

Response: Detect encoding of the current language of the current language of the current language.


Instruction:
Create a Python 3 function for
calling when the mode is activated or deactivated

Response:
def on_state_changed(self, state):
        """
        Called when the mode is activated/deactivated
        """
        if state:
            self.action.triggered.connect(self.comment)
            self.editor.add_action(self.action, sub_menu='Python')
            if 'pyqt5' in os.environ['QT_API'].lower():
                self.editor.key_pressed.connect(self.on_key_pressed)
        else:
            self.editor.remove_action(self.action, sub_menu='Python')
            self.action.triggered.disconnect(self.comment)
            if 'pyqt5' in os.environ['QT_API'].lower():
                self.editor.key_pressed.disconnect(self.on_key_pressed)


Instruction:
Write a Python 3 function that can
comment the selected lines or the current lines if there is no selection.

Response:
def comment(self):
        """
        Comments/Uncomments the selected lines or the current lines if there
        is no selection.
        """
        cursor = self.editor.textCursor()
        # get the indent at which comment should be inserted and whether to
        # comment or uncomment the selected text
        indent, comment, nb_lines = self.get_operation()
        has_selection = cursor.hasSelection()
        if nb_lines > 1:
            self._move_cursor_to_selection_start(cursor)
            cursor.beginEditBlock()
            for i in range(nb_lines):
                self.comment_line(indent, cursor, comment)
                cursor.movePosition(cursor.NextBlock)
            cursor.endEditBlock()
        else:
            # comment a single line
            cursor.beginEditBlock()
            self.comment_line(indent, cursor, comment)
            if not has_selection:
                # move to the first non-whitespace character of the next line
                cursor.movePosition(cursor.NextBlock)
                text = cursor.block().text()
                indent = len(text) - len(text.lstrip())
                cursor.movePosition(cursor.Right, cursor.MoveAnchor, indent)
                cursor.endEditBlock()
                self.editor.setTextCursor(cursor)
            else:
                cursor.endEditBlock()


Instruction:
Explain what the following Python 3 code does
def setPlainText(self, txt, mimetype='text/x-python', encoding='utf-8'):
        try:
            self.syntax_highlighter.docstrings[:] = []
            self.syntax_highlighter.import_statements[:] = []
        except AttributeError:
            pass
        super(PyCodeEditBase, self).setPlainText(txt, mimetype, encoding)

Response: Extends QCodeEdit. setPlainText to allow user to set plain text without mimetype.


Instruction:
Make a summary of the following Python 3 code
def update_terminal_colors(self):
        self.color_scheme = self.create_color_scheme(
            background=self.syntax_highlighter.color_scheme.background,
            foreground=self.syntax_highlighter.color_scheme.formats['normal'].foreground().color())

Response: Update terminal colors based on pygments color scheme colors


Instruction:
Given the following Python 3 function, write the documentation
def mouseMoveEvent(self, e):
        super(PyInteractiveConsole, self).mouseMoveEvent(e)
        cursor = self.cursorForPosition(e.pos())
        assert isinstance(cursor, QtGui.QTextCursor)
        p = cursor.positionInBlock()
        usd = cursor.block().userData()
        if usd and usd.start_pos_in_block <= p <= usd.end_pos_in_block:
            if QtWidgets.QApplication.overrideCursor() is None:
                QtWidgets.QApplication.setOverrideCursor(
                    QtGui.QCursor(QtCore.Qt.PointingHandCursor))
        else:
            if QtWidgets.QApplication.overrideCursor() is not None:
                QtWidgets.QApplication.restoreOverrideCursor()

Response: Overrides mouseMoveEvent to display a pointing hand cursor when the mouse cursor is over a file location


Instruction:
How would you explain what the following Python 3 function does
def mousePressEvent(self, e):
        super(PyInteractiveConsole, self).mousePressEvent(e)
        cursor = self.cursorForPosition(e.pos())
        p = cursor.positionInBlock()
        usd = cursor.block().userData()
        if usd and usd.start_pos_in_block <= p <= usd.end_pos_in_block:
            if e.button() == QtCore.Qt.LeftButton:
                self.open_file_requested.emit(usd.filename, usd.line)

Response:
Emits open_file_requested if the press event occured over
        a file location string.


Instruction:
Can you generate the documentation for the following Python 3 function
def detect_fold_level(self, prev_block, block):
        # Python is an indent based language so use indentation for folding
        # makes sense but we restrict new regions to indentation after a ':',
        # that way only the real logical blocks are displayed.
        lvl = super(PythonFoldDetector, self).detect_fold_level(
            prev_block, block)
        # cancel false indentation, indentation can only happen if there is
        # ':' on the previous line
        prev_lvl = TextBlockHelper.get_fold_lvl(prev_block)
        if prev_block and lvl > prev_lvl and not (
                self._strip_comments(prev_block).endswith(':')):
            lvl = prev_lvl
        lvl = self._handle_docstrings(block, lvl, prev_block)
        lvl = self._handle_imports(block, lvl, prev_block)
        return lvl

Response: Detects the fold level of the current block.


Instruction:
Can you generate the documentation for the following Python 3 function
def setup_actions(self):
        self.actionOpen.triggered.connect(self.on_open)
        self.actionNew.triggered.connect(self.on_new)
        self.actionSave.triggered.connect(self.on_save)
        self.actionSave_as.triggered.connect(self.on_save_as)
        self.actionQuit.triggered.connect(
            QtWidgets.QApplication.instance().quit)
        self.tabWidget.current_changed.connect(self.on_current_tab_changed)
        self.tabWidget.last_tab_closed.connect(self.on_last_tab_closed)
        self.actionAbout.triggered.connect(self.on_about)
        self.actionRun.triggered.connect(self.on_run)
        self.interactiveConsole.process_finished.connect(
            self.on_process_finished)
        self.actionConfigure_run.triggered.connect(self.on_configure_run)

Response: Connects the slots to signals


Instruction:
Given the following Python 3 function, write the documentation
def open_file(self, path, line=None):
        editor = None
        if path:
            interpreter, pyserver, args = self._get_backend_parameters()
            editor = self.tabWidget.open_document(
                path, None, interpreter=interpreter, server_script=pyserver,
                args=args)
            if editor:
                self.setup_editor(editor)
            self.recent_files_manager.open_file(path)
            self.menu_recents.update_actions()
        if line is not None:
            TextHelper(self.tabWidget.current_widget()).goto_line(line)
        return editor

Response: Opens a file and adds it to the menu.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def _get_backend_parameters(self):
        frozen = hasattr(sys, 'frozen')
        interpreter = Settings().interpreter
        if frozen:
            interpreter = None
        pyserver = server.__file__ if interpreter is not None else 'server.exe'
        args = []
        return interpreter, pyserver, args

Response: Gets the interpreter pyserver and args for the backend.


Instruction:
Write a Python 3 function for
adding a new empty code editor to the tab widget

Response:
def on_new(self):
        """
        Add a new empty code editor to the tab widget
        """
        interpreter, pyserver, args = self._get_backend_parameters()
        self.setup_editor(self.tabWidget.create_new_document(
            extension='.py', interpreter=interpreter, server_script=pyserver,
            args=args))
        self.actionRun.setDisabled(True)
        self.actionConfigure_run.setDisabled(True)


Instruction:
Write a Python 3 function for
opening file dialog and open the file if the dialog was accepted.

Response:
def on_open(self):
        """
        Shows an open file dialog and open the file if the dialog was
        accepted.

        """
        filename, filter = QtWidgets.QFileDialog.getOpenFileName(self, 'Open')
        if filename:
            self.open_file(filename)
        self.actionRun.setEnabled(True)
        self.actionConfigure_run.setEnabled(True)


Instruction:
Here you have a function in Python 3, explain what it does
def on_save_as(self):
        path = self.tabWidget.current_widget().file.path
        path = os.path.dirname(path) if path else ''
        filename, filter = QtWidgets.QFileDialog.getSaveFileName(
            self, 'Save', path)
        if filename:
            self.tabWidget.save_current(filename)
            self.recent_files_manager.open_file(filename)
            self.menu_recents.update_actions()
            self.actionRun.setEnabled(True)
            self.actionConfigure_run.setEnabled(True)
            self._update_status_bar(self.tabWidget.current_widget())

Response: Save the current editor document as.


Instruction:
Can you tell what is the following Python 3 function doing
def setup_mnu_panels(self, editor):
        for panel in editor.panels:
            if panel.dynamic:
                continue
            a = QtWidgets.QAction(self.menuModes)
            a.setText(panel.name)
            a.setCheckable(True)
            a.setChecked(panel.enabled)
            a.changed.connect(self.on_panel_state_changed)
            a.panel = weakref.proxy(panel)
            self.menuPanels.addAction(a)

Response: Setup the panels menu for the current editor.


Instruction:
How would you explain what the following Python 3 function does
def on_current_tab_changed(self):
        self.menuEdit.clear()
        self.menuModes.clear()
        self.menuPanels.clear()
        editor = self.tabWidget.current_widget()
        self.menuEdit.setEnabled(editor is not None)
        self.menuModes.setEnabled(editor is not None)
        self.menuPanels.setEnabled(editor is not None)
        self.actionSave.setEnabled(editor is not None)
        self.actionSave_as.setEnabled(editor is not None)
        self.actionConfigure_run.setEnabled(editor is not None)
        self.actionRun.setEnabled(editor is not None)
        if editor is not None:
            self.setup_mnu_edit(editor)
            self.setup_mnu_modes(editor)
            self.setup_mnu_panels(editor)
        self.widgetOutline.set_editor(editor)
        self._update_status_bar(editor)

Response: Update action states when the current tab has changed.


Instruction:
Given the following Python 3 function, write the documentation
def on_run(self):
        filename = self.tabWidget.current_widget().file.path
        wd = os.path.dirname(filename)
        args = Settings().get_run_config_for_file(filename)
        self.interactiveConsole.start_process(
            Settings().interpreter, args=[filename] + args, cwd=wd)
        self.dockWidget.show()
        self.actionRun.setEnabled(False)
        self.actionConfigure_run.setEnabled(False)

Response: Run the current script


Instruction:
Given the following Python 3 function, write the documentation
def on_goto_out_of_doc(self, assignment):
        editor = self.open_file(assignment.module_path)
        if editor:
            TextHelper(editor).goto_line(assignment.line, assignment.column)

Response: Open a new tab when goto goes out of the current document.


Instruction:
Can you write a function in Python 3 where it
goes to assignements worker.

Response:
def goto_assignments(request_data):
    """
    Go to assignements worker.
    """
    code = request_data['code']
    line = request_data['line'] + 1
    column = request_data['column']
    path = request_data['path']
    # encoding = request_data['encoding']
    encoding = 'utf-8'
    script = jedi.Script(code, line, column, path, encoding)
    try:
        definitions = script.goto_assignments()
    except jedi.NotFoundError:
        pass
    else:
        ret_val = [(d.module_path, d.line - 1 if d.line else None,
                    d.column, d.full_name)
                   for d in definitions]
        return ret_val


Instruction:
Explain what the following Python 3 code does
def defined_names(request_data):
    global _old_definitions
    ret_val = []
    path = request_data['path']
    toplvl_definitions = jedi.names(
        request_data['code'], path, 'utf-8')
    for d in toplvl_definitions:
        definition = _extract_def(d, path)
        if d.type != 'import':
            ret_val.append(definition)
    ret_val = [d.to_dict() for d in ret_val]
    return ret_val

Response: Returns the list of defined names for the document.


Instruction:
Can you generate the documentation for the following Python 3 function
def quick_doc(request_data):
    code = request_data['code']
    line = request_data['line'] + 1
    column = request_data['column']
    path = request_data['path']
    # encoding = 'utf-8'
    encoding = 'utf-8'
    script = jedi.Script(code, line, column, path, encoding)
    try:
        definitions = script.goto_definitions()
    except jedi.NotFoundError:
        return []
    else:
        ret_val = [d.docstring() for d in definitions]
        return ret_val

Response: Worker that returns the documentation of the symbol under cursor.


Instruction:
How would you explain what the following Python 3 function does
def run_pep8(request_data):
    import pycodestyle
    from pyqode.python.backend.pep8utils import CustomChecker
    WARNING = 1
    code = request_data['code']
    path = request_data['path']
    max_line_length = request_data['max_line_length']
    ignore_rules = request_data['ignore_rules']
    ignore_rules += ['W291', 'W292', 'W293', 'W391']
    pycodestyle.MAX_LINE_LENGTH = max_line_length
    # setup our custom style guide with our custom checker which returns a list
    # of strings instread of spitting the results at stdout
    pep8style = pycodestyle.StyleGuide(parse_argv=False, config_file='',
                                       checker_class=CustomChecker)
    try:
        results = pep8style.input_file(path, lines=code.splitlines(True))
    except Exception:
        _logger().exception('Failed to run PEP8 analysis with data=%r'
                            % request_data)
        return []
    else:
        messages = []
        for line_number, offset, code, text, doc in results:
            if code in ignore_rules:
                continue
            messages.append(('[PEP8] %s: %s' % (code, text), WARNING,
                             line_number - 1))
        return messages

Response: This function runs the PEP8 tool on the current editor text and returns a list of tuples.


Instruction:
Can you write a function in Python 3 where it
returns the icon resource filename that corresponds to the given typename.

Response:
def icon_from_typename(name, icon_type):
    """
    Returns the icon resource filename that corresponds to the given typename.

    :param name: name of the completion. Use to make the distinction between
        public and private completions (using the count of starting '_')
    :pram typename: the typename reported by jedi

    :returns: The associate icon resource filename or None.
    """
    ICONS = {
        'CLASS': ICON_CLASS,
        'IMPORT': ICON_NAMESPACE,
        'STATEMENT': ICON_VAR,
        'FORFLOW': ICON_VAR,
        'FORSTMT': ICON_VAR,
        'WITHSTMT': ICON_VAR,
        'GLOBALSTMT': ICON_VAR,
        'MODULE': ICON_NAMESPACE,
        'KEYWORD': ICON_KEYWORD,
        'PARAM': ICON_VAR,
        'ARRAY': ICON_VAR,
        'INSTANCEELEMENT': ICON_VAR,
        'INSTANCE': ICON_VAR,
        'PARAM-PRIV': ICON_VAR,
        'PARAM-PROT': ICON_VAR,
        'FUNCTION': ICON_FUNC,
        'DEF': ICON_FUNC,
        'FUNCTION-PRIV': ICON_FUNC_PRIVATE,
        'FUNCTION-PROT': ICON_FUNC_PROTECTED
    }
    ret_val = None
    icon_type = icon_type.upper()
    # jedi 0.8 introduced NamedPart class, which have a string instead of being
    # one
    if hasattr(name, "string"):
        name = name.string
    if icon_type == "FORFLOW" or icon_type == "STATEMENT":
        icon_type = "PARAM"
    if icon_type == "PARAM" or icon_type == "FUNCTION":
        if name.startswith("__"):
            icon_type += "-PRIV"
        elif name.startswith("_"):
            icon_type += "-PROT"
    if icon_type in ICONS:
        ret_val = ICONS[icon_type]
    elif icon_type:
        _logger().warning("Unimplemented completion icon_type: %s", icon_type)
    return ret_val


Instruction:
Write a Python 3 script to
complete python code using jedi.

Response:
def complete(code, line, column, path, encoding, prefix):
        """
        Completes python code using `jedi`_.

        :returns: a list of completion.
        """
        ret_val = []
        try:
            script = jedi.Script(code, line + 1, column, path, encoding)
            completions = script.completions()
            print('completions: %r' % completions)
        except jedi.NotFoundError:
            completions = []
        for completion in completions:
            ret_val.append({
                'name': completion.name,
                'icon': icon_from_typename(
                    completion.name, completion.type),
                'tooltip': completion.description})
        return ret_val


Instruction:
Create a Python 3 function for
requesting a go to the assignment.

Response:
def _check_word_cursor(self, tc=None):
        """
        Request a go to assignment.

        :param tc: Text cursor which contains the text that we must look for
                   its assignment. Can be None to go to the text that is under
                   the text cursor.
        :type tc: QtGui.QTextCursor
        """
        if not tc:
            tc = TextHelper(self.editor).word_under_cursor()

        request_data = {
            'code': self.editor.toPlainText(),
            'line': tc.blockNumber(),
            'column': tc.columnNumber(),
            'path': self.editor.file.path,
            'encoding': self.editor.file.encoding
        }
        try:
            self.editor.backend.send_request(
                workers.goto_assignments, request_data,
                on_receive=self._on_results_available)
        except NotRunning:
            pass


Instruction:
Make a summary of the following Python 3 code
def _unique(self, seq):
        # order preserving
        checked = []
        for e in seq:
            present = False
            for c in checked:
                if str(c) == str(e):
                    present = True
                    break
            if not present:
                checked.append(e)
        return checked

Response: Returns a list of unique items in the sequence.


Instruction:
Create a Python 3 function to
validate the list of directives.

Response:
def _validateDirectives(self, directiveList, checkFileName):

        if len(directiveList) == 0:
            raise ParsingException("'{file}' does not contain any CHECK directives".format(file=checkFileName))

        from . import Directives
        """
            We should enforce for every CHECK-NOT and CHECK-NOT-L directive that the next directive (if it exists) is
            a CHECK or CHECK-L directive
        """
        last = len(directiveList) -1
        supportedDirectives = [ Directives.Check, Directives.CheckLiteral ]
        for (index,directive) in enumerate(directiveList):
            if isA(directive, [Directives.CheckNot, Directives.CheckNotLiteral]):
                if index < last:
                    after = directiveList[index +1]
                    if not isA(after, supportedDirectives):
                        requiredTypes = " or ".join( [ "CHECK{suffix}".format(suffix=d.directiveToken()) for d in supportedDirectives])
                        raise ParsingException("{directive} must have a {requiredTypes} directive after it instead of a {bad}".format(
                                                  directive=directive,
                                                  requiredTypes=requiredTypes,
                                                  check=Directives.Check.directiveToken(),
                                                  bad=after)
                                              )


Instruction:
Can you tell what is the following Python 3 function doing
def _substituteCheckPattern(self, inputString, lineNumber, lastLineNumber, checkFileName, isForRegex):
        assert isinstance(inputString, str)
        assert isinstance(lineNumber, int)
        assert isinstance(lastLineNumber, int)
        assert isinstance(checkFileName, str)

        """
        Do ${LINE}, ${LINE:+N}, and ${LINE:-N} substitutions.
        To escape prepend with slash
        """
        sPattern = r'\$\{LINE(\:(?P<sign>\+|-)(?P<offset>\d+))?\}'
        matcher = re.compile(sPattern)
        result = ""
        loop = True
        start = 0
        end = len(inputString) # Not inclusive
        while loop:
            m = matcher.search(inputString, start, end)
            if not m:
                # No match so copy verbatim
                _logger.debug('Result is currently "{}"'.format(result))
                result += inputString[start:end]
                break # And we're done :)
            else:
                prevIndex = max(0, m.start() -1)
                _logger.debug('Previous character before match is at index {index} "{char}"'.format(index=prevIndex, char=inputString[prevIndex]))
                if inputString[prevIndex] == "\\":
                    # User asked to escape
                    _logger.debug('Substitution is escaped')
                    _logger.debug('Result is currently "{}"'.format(result))
                    result += inputString[start:prevIndex] # Copy before escaping character
                    _logger.debug('Result is currently "{}"'.format(result))
                    result += inputString[(prevIndex+1):m.end()] # Copy the ${LINE..} verbatim
                    start = min(m.end(), end)
                    _logger.debug('Result is currently "{}"'.format(result))
                    _logger.debug('Next search is {start}:{end} = "{ss}"'.format(start=start, end=end, ss=inputString[start:end]))
                else:
                    _logger.debug('Result is currently "{}"'.format(result))
                    _logger.debug('Doing subsitution. Found at {begin}:{end} = {ss}'.format(begin=m.start(),end=m.end(), ss=inputString[m.start():m.end()]))
                    result += inputString[start:m.start()] # Copy before substitution starts

                    if m.groupdict()['sign'] == None:
                        # No offset just substitute line number
                        _logger.debug('No offset')
                        result += str(lineNumber)
                    else:
                        offset = 1 if m.groupdict()['sign'] == '+' else -1
                        offset *= int(m.groupdict()['offset'])
                        _logger.debug('Offset is {}'.format(offset))

                        requestedLineNumber = lineNumber + offset
                        _logger.debug('Request line number to print is  {}'.format(requestedLineNumber))

                        if requestedLineNumber <= 0:
                            raise ParsingException('{file}:{line}:{col} offset gives line number < 1'.format(file=checkFileName, line=lineNumber, col=m.start()))
                        elif requestedLineNumber > lastLineNumber:
                            raise ParsingException('{file}:{line}:{col} offset gives line number past the end of file'.format(file=checkFileName, line=lineNumber, col=m.start()))

                        result += str(requestedLineNumber)

                    start = min(m.end(),end)
                    _logger.debug('Next search is {start}:{end} = "{ss}"'.format(start=start, end=end, ss=inputString[start:end]))

        """
        Do simple ${...} substitutions
        """

        # Do ${CHECKFILE_NAME} substitution
        basenameCheckFileName = os.path.basename(checkFileName)
        assert basenameCheckFileName.count('\\') == 0
        result = self._simpleSubstitution("CHECKFILE_NAME", basenameCheckFileName, result)

        # Do ${CHECKFILE_ABS_PATH} substitution
        abspathCheckFileName = os.path.abspath(checkFileName)
        if isForRegex:
            # Note slash substitution is for Windows paths (e.g. "c:\mything\foo.txt") which can break regexes if we don't
            # correctly escape them.
            abspathCheckFileName = abspathCheckFileName.replace('\\', '\\\\')

        result = self._simpleSubstitution("CHECKFILE_ABS_PATH", abspathCheckFileName, result)

        assert len(result) != 0
        return result

Response: Substitute checkPattern in the inputString.


Instruction:
Here you have a function in Python 3, explain what it does
def create_metafile(bgen_filepath, metafile_filepath, verbose=True):
    if verbose:
        verbose = 1
    else:
        verbose = 0

    bgen_filepath = make_sure_bytes(bgen_filepath)
    metafile_filepath = make_sure_bytes(metafile_filepath)

    assert_file_exist(bgen_filepath)
    assert_file_readable(bgen_filepath)

    if exists(metafile_filepath):
        raise ValueError(f"The file {metafile_filepath} already exists.")

    with bgen_file(bgen_filepath) as bgen:
        nparts = _estimate_best_npartitions(lib.bgen_nvariants(bgen))
        metafile = lib.bgen_create_metafile(bgen, metafile_filepath, nparts, verbose)
        if metafile == ffi.NULL:
            raise RuntimeError(f"Error while creating metafile: {metafile_filepath}.")

        if lib.bgen_close_metafile(metafile) != 0:
            raise RuntimeError(f"Error while closing metafile: {metafile_filepath}.")

Response: r Create variants metadata file.


Instruction:
Make a summary of the following Python 3 code
def match(self, subsetLines, offsetOfSubset, fileName):

        for (offset,l) in enumerate(subsetLines):
            column = l.find(self.literal)
            if column != -1:
                truePosition = offset + offsetOfSubset
                _logger.debug('Found match on line {}, col {}'.format(str(truePosition+ 1), column))
                _logger.debug('Line is {}'.format(l))
                self.matchLocation = CheckFileParser.FileLocation(fileName, truePosition +1)
                return truePosition

        # No Match found
        self.failed = True
        raise DirectiveException(self)

Response: Match the line with the literal in the line that matches the pattern.


Instruction:
Implement a function in Python 3 to
search through lines for match. A match is found.

Response:
def match(self, subsetLines, offsetOfSubset, fileName):
        """
            Search through lines for match.
            Raise an Exception if a match
        """
        for (offset,l) in enumerate(subsetLines):
            for t in self.regex:
                m = t.Regex.search(l)
                if m != None:
                    truePosition = offset + offsetOfSubset
                    _logger.debug('Found match on line {}'.format(str(truePosition+ 1)))
                    _logger.debug('Line is {}'.format(l))
                    self.failed = True
                    self.matchLocation = CheckFileParser.FileLocation(fileName, truePosition +1)
                    raise DirectiveException(self)


Instruction:
How would you explain what the following Python 3 function does
def isA(instance, typeList):
    return any(map(lambda iType: isinstance(instance,iType), typeList))

Response:
Return True if instance is an instance of any of the Directive
        types in typeList.


Instruction:
Create a Python 3 function for
touching a file. Taxonomy Credits to <https://stackoverflow. com/a/1160227 >.

Response:
def _touch(fname, mode=0o666, dir_fd=None, **kwargs):
    """ Touch a file.

    Credits to <https://stackoverflow.com/a/1160227>.
    """
    flags = os.O_CREAT | os.O_APPEND
    with os.fdopen(os.open(fname, flags=flags, mode=mode, dir_fd=dir_fd)) as f:
        os.utime(
            f.fileno() if os.utime in os.supports_fd else fname,
            dir_fd=None if os.supports_fd else dir_fd,
            **kwargs,
        )


Instruction:
How would you code a function in Python 3 to
try to infer the correct library name.

Response:
def find_libname(self, name):
        """Try to infer the correct library name."""
        names = ["{}.lib", "lib{}.lib", "{}lib.lib"]
        names = [n.format(name) for n in names]
        dirs = self.get_library_dirs()
        for d in dirs:
            for n in names:
                if exists(join(d, n)):
                    return n[:-4]
        msg = "Could not find the {} library.".format(name)
        raise ValueError(msg)


Instruction:
Make a summary of the following Python 3 code
def split(self, X, y=None, groups=None):
        X, y, groups = indexable(X, y, groups)
        cgrs = [~r for r in X]

        structure_condition = defaultdict(set)

        for structure, condition in zip(cgrs, groups):
            structure_condition[structure].add(condition)

        train_data = defaultdict(list)
        test_data = []

        for n, (structure, condition) in enumerate(zip(cgrs, groups)):
            train_data[condition].append(n)
            if len(structure_condition[structure]) > 1:
                test_data.append(n)

        for condition, indexes in train_data.items():
            test_index = [index for index in indexes if index in test_data]
            if test_index:
                train_index = [i for cond, ind in train_data.items() if cond != condition for i in ind]
                yield array(train_index), array(test_index)

Response: Generates indices to split data into training and test sets.


Instruction:
Can you generate the documentation for the following Python 3 function
def molconvert_chemaxon(data):
    if isinstance(data, Path):
        with data.open('rb') as f:
            data = f.read()
    elif isinstance(data, StringIO):
        data = data.read().encode()
    elif isinstance(data, BytesIO):
        data = data.read()
    elif hasattr(data, 'read'):  # check if data is open(filename, mode)
        data = data.read()
        if isinstance(data, str):
            data = data.encode()
    elif isinstance(data, str):
        data = data.encode()
    elif not isinstance(data, bytes):
        raise ValueError('invalid input')

    try:
        p = run(['molconvert', '-g', 'mrv'], input=data, stdout=PIPE)
    except FileNotFoundError as e:
        raise ConfigurationError from e

    if p.returncode != 0:
        raise ConfigurationError(p.stderr.decode())

    with BytesIO(p.stdout) as f, MRVread(f) as r:
        return iter2array(r)

Response: molconvert wrapper for the chemaxon file atomconvert wrapper


Instruction:
Create a Python 3 function for
fitting distance - based AD.

Response:
def fit(self, X, y=None):
        """Fit distance-based AD.

        Parameters
        ----------
        X : array-like or sparse matrix, shape (n_samples, n_features)
            The input samples. Use ``dtype=np.float32`` for maximum
            efficiency.

        Returns
        -------
        self : object
            Returns self.
        """
        # Check data
        X = check_array(X)
        self.tree = BallTree(X, leaf_size=self.leaf_size, metric=self.metric)
        dist_train = self.tree.query(X, k=2)[0]
        if self.threshold == 'auto':
            self.threshold_value = 0.5 * sqrt(var(dist_train[:, 1])) + mean(dist_train[:, 1])
        elif self.threshold == 'cv':
            if y is None:
                raise ValueError("Y must be specified to find the optimal threshold.")
            y = check_array(y, accept_sparse='csc', ensure_2d=False, dtype=None)
            self.threshold_value = 0
            score = 0
            Y_pred, Y_true, AD = [], [], []
            cv = KFold(n_splits=5, random_state=1, shuffle=True)
            for train_index, test_index in cv.split(X):
                x_train = safe_indexing(X, train_index)
                x_test = safe_indexing(X, test_index)
                y_train = safe_indexing(y, train_index)
                y_test = safe_indexing(y, test_index)
                data_test = safe_indexing(dist_train[:, 1], test_index)
                if self.reg_model is None:
                    reg_model = RandomForestRegressor(n_estimators=500, random_state=1).fit(x_train, y_train)
                else:
                    reg_model = clone(self.reg_model).fit(x_train, y_train)
                Y_pred.append(reg_model.predict(x_test))
                Y_true.append(y_test)
                AD.append(data_test)
            AD_ = unique(hstack(AD))
            for z in AD_:
                AD_new = hstack(AD) <= z
                if self.score == 'ba_ad':
                    val = balanced_accuracy_score_with_ad(Y_true=hstack(Y_true), Y_pred=hstack(Y_pred), AD=AD_new)
                elif self.score == 'rmse_ad':
                    val = rmse_score_with_ad(Y_true=hstack(Y_true), Y_pred=hstack(Y_pred), AD=AD_new)
                if val >= score:
                    score = val
                    self.threshold_value = z
        else:
            self.threshold_value = self.threshold
        return self


Instruction:
Can you generate the documentation for the following Python 3 function
def predict_proba(self, X):
        # Check is fit had been called
        check_is_fitted(self, ['tree'])
        # Check data
        X = check_array(X)
        return self.tree.query(X)[0].flatten()

Response: Returns the value of the nearest neighbor from the training set.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def predict(self, X):
        # Check is fit had been called
        check_is_fitted(self, ['tree'])
        # Check data
        X = check_array(X)
        return self.tree.query(X)[0].flatten() <= self.threshold_value

Response: Predict if a particular sample is an outlier or not.


Instruction:
Create a Python 3 function for
fitting the regression model on the set of classes.

Response:
def fit(self, X, y=None):
        """Learning is to find the inverse matrix for X and calculate the threshold.

        Parameters
        ----------
        X : array-like or sparse matrix, shape (n_samples, n_features)
            The input samples. Use ``dtype=np.float32`` for maximum
            efficiency.
        y : array-like, shape = [n_samples] or [n_samples, n_outputs]
            The target values (real numbers in regression).

        Returns
        -------
        self : object
        """
        # Check that X have correct shape
        X = check_array(X)
        self.inverse_influence_matrix = self.__make_inverse_matrix(X)
        if self.threshold == 'auto':
            self.threshold_value = 3 * (1 + X.shape[1]) / X.shape[0]
        elif self.threshold == 'cv':
            if y is None:
                raise ValueError("Y must be specified to find the optimal threshold.")
            y = check_array(y, accept_sparse='csc', ensure_2d=False, dtype=None)
            self.threshold_value = 0
            score = 0
            Y_pred, Y_true, AD = [], [], []
            cv = KFold(n_splits=5, random_state=1, shuffle=True)
            for train_index, test_index in cv.split(X):
                x_train = safe_indexing(X, train_index)
                x_test = safe_indexing(X, test_index)
                y_train = safe_indexing(y, train_index)
                y_test = safe_indexing(y, test_index)
                if self.reg_model is None:
                    reg_model = RandomForestRegressor(n_estimators=500, random_state=1).fit(x_train, y_train)
                else:
                    reg_model = clone(self.reg_model).fit(x_train, y_train)
                Y_pred.append(reg_model.predict(x_test))
                Y_true.append(y_test)
                ad_model = self.__make_inverse_matrix(x_train)
                AD.append(self.__find_leverages(x_test, ad_model))
            AD_ = unique(hstack(AD))
            for z in AD_:
                AD_new = hstack(AD) <= z
                if self.score == 'ba_ad':
                    val = balanced_accuracy_score_with_ad(Y_true=hstack(Y_true), Y_pred=hstack(Y_pred), AD=AD_new)
                elif self.score == 'rmse_ad':
                    val = rmse_score_with_ad(Y_true=hstack(Y_true), Y_pred=hstack(Y_pred), AD=AD_new)
                if val >= score:
                    score = val
                    self.threshold_value = z
        else:
            self.threshold_value = self.threshold
        return self


Instruction:
Here you have a function in Python 3, explain what it does
def predict_proba(self, X):
        # Check is fit had been called
        check_is_fitted(self, ['inverse_influence_matrix'])
        # Check that X have correct shape
        X = check_array(X)
        return self.__find_leverages(X, self.inverse_influence_matrix)

Response: Predict the distances for X to the center of the training set.


Instruction:
Explain what the following Python 3 code does
def predict(self, X):
        # Check is fit had been called
        check_is_fitted(self, ['inverse_influence_matrix'])
        # Check that X have correct shape
        X = check_array(X)
        return self.__find_leverages(X, self.inverse_influence_matrix) <= self.threshold_value

Response: Predicts inside or outside AD for X.


Instruction:
How would you explain what the following Python 3 function does
def get_feature_names(self):
        return ['temperature', 'pressure'] + [f'solvent.{x}' for x in range(1, self.max_solvents + 1)] + \
               [f'solvent_amount.{x}' for x in range(1, self.max_solvents + 1)]

Response:
Get feature names.

        Returns
        -------
        feature_names : list of strings
            Names of the features produced by transform.


Instruction:
Write a Python 3 function for
fitting the object to the given data.

Response:
def fit(self, X, y=None):
        """Find min and max values of every feature.

        Parameters
        ----------
        X : {array-like, sparse matrix}, shape (n_samples, n_features)
            The training input samples.
        y : Ignored
            not used, present for API consistency by convention.

        Returns
        -------
        self : object
        """
        # Check that X have correct shape
        X = check_array(X)

        self._x_min = X.min(axis=0) # axis=0 will find the minimum values by columns (for each feature)
        self._x_max = X.max(axis=0) # axis=0 will find the minimum values by columns (for each feature)
        return self


Instruction:
Make a summary of the following Python 3 code
def predict(self, X):
        # Check is fit had been called
        check_is_fitted(self, ['_x_min', '_x_max'])

        # Input validation
        X = check_array(X)
        return ((X - self._x_min).min(axis=1) >= 0) & ((self._x_max - X).min(axis=1) >= 0)

Response: Predict if a particular sample is an outlier or not.


Instruction:
How would you implement a function in Python 3 that
generates indices to split the data into training and test sets.

Response:
def split(self, X, y=None, groups=None):
        """Generate indices to split data into training and test set.
        Parameters
        ----------
        X : array-like, of length n_samples
            Training data, includes reaction's containers
        y : array-like, of length n_samples
            The target variable for supervised learning problems.
        groups : array-like, with shape (n_samples,)
            Group labels for the samples used while splitting the dataset into
            train/test set.
        Yields
        ------
        train : ndarray
            The training set indices for that split.
        test : ndarray
            The testing set indices for that split.
        """
        X, y, groups = indexable(X, y, groups)
        cgrs = [~r for r in X]

        condition_structure = defaultdict(set)

        for structure, condition in zip(cgrs, groups):
            condition_structure[condition].add(structure)

        train_data = defaultdict(list)
        test_data = []

        for n, (structure, condition) in enumerate(zip(cgrs, groups)):
            train_data[structure].append(n)
            if len(condition_structure[condition]) > 1:
                test_data.append(n)

        if self.n_splits > len(train_data):
            raise ValueError("Cannot have number of splits n_splits=%d greater"
                             " than the number of transformations: %d."
                             % (self.n_splits, len(train_data)))

        structures_weight = sorted(((x, len(y)) for x, y in train_data.items()), key=lambda x: x[1], reverse=True)
        fold_mean_size = len(cgrs) // self.n_splits

        if structures_weight[0][1] > fold_mean_size:
            warning('You have transformation that greater fold size')

        for idx in range(self.n_repeats):
            train_folds = [[] for _ in range(self.n_splits)]
            for structure, structure_length in structures_weight:
                if self.shuffle:
                    check_random_state(self.random_state).shuffle(train_folds)
                for fold in train_folds[:-1]:
                    if len(fold) + structure_length <= fold_mean_size:
                        fold.extend(train_data[structure])
                        break
                    else:
                        roulette_param = (structure_length - fold_mean_size + len(fold)) / structure_length
                        if random() > roulette_param:
                            fold.extend(train_data[structure])
                            break
                else:
                    train_folds[-1].extend(train_data[structure])

            test_folds = [[] for _ in range(self.n_splits)]
            for test, train in zip(test_folds, train_folds):
                for index in train:
                    if index in test_data:
                        test.append(index)

            for i in range(self.n_splits):
                train_index = []
                for fold in train_folds[:i]:
                    train_index.extend(fold)
                for fold in train_folds[i+1:]:
                    train_index.extend(fold)
                test_index = test_folds[i]
                yield array(train_index), array(test_index)


Instruction:
Write a Python 3 script for
doing nothing and return the estimator unchanged

Response:
def fit(self, x, y=None):
        """Do nothing and return the estimator unchanged

        This method is just there to implement the usual API and hence work in pipelines.
        """
        if self._dtype is not None:
            iter2array(x, dtype=self._dtype)
        else:
            iter2array(x)
        return self


Instruction:
Can you tell what is the following Python 3 function doing
def finalize(self):
        if self.__head_less:
            warn(f'{self.__class__.__name__} configured to head less mode. finalize unusable')
        elif not self.__head_generate:
            warn(f'{self.__class__.__name__} already finalized or fitted')
        elif not self.__head_dict:
            raise NotFittedError(f'{self.__class__.__name__} instance is not fitted yet')
        else:
            if self.remove_rare_ratio:
                self.__clean_head(*self.__head_rare)
                self.__prepare_header()
                self.__head_rare = None
            self.__head_generate = False

Response: finalize partial fitting procedure


Instruction:
Can you generate the documentation for the following Python 3 function
def _reset(self):
        if not self.__head_less:
            if not self.__head_generate:
                self.__head_generate = True
            if self.__head_dict:
                self.__head_dump = self.__head_dict = None
            if self.__head_rare is not None:
                self.__head_rare = None

            self.delete_work_path()

Response: Reset internal data - dependent state.


Instruction:
Can you generate the documentation for the following Python 3 function
def get_feature_names(self):
        if self.__head_less:
            raise AttributeError(f'{self.__class__.__name__} instance configured to head less mode')
        elif not self.__head_dict:
            raise NotFittedError(f'{self.__class__.__name__} instance is not fitted yet')
        return list(self.__head_dict.values())

Response:
Get feature names.

        Returns
        -------
        feature_names : list of strings
            Names of the features produced by transform.


Instruction:
How would you explain what the following Python 3 function does
def fit(self, X):
        X = iter2array(X, dtype=ReactionContainer)
        self._train_signatures = {self.__get_signature(x) for x in X}
        return self

Response: Fit structure - based AD. The training model memorizes the unique set of reaction signatures.


Instruction:
Implement a Python 3 function for
predicting reaction for given set of training sets.

Response:
def predict(self, X):
        """Reaction is considered belonging to model’s AD
        if its reaction signature coincides with ones used in training set.

        Parameters
        ----------
        X : after read rdf file

        Returns
        -------
        self : array contains True (reaction in AD) and False (reaction residing outside AD).
        """
        check_is_fitted(self, ['_train_signatures'])
        X = iter2array(X, dtype=ReactionContainer)
        return array([self.__get_signature(x) in self._train_signatures for x in X])


Instruction:
Create a Python 3 function to
parse an expression into a list of identifiers and tokens

Response:
def __parser(expression):
        """ adopted from Paul McGuire example. http://pyparsing.wikispaces.com/file/view/fourFn.py
        """
        expr_stack = []

        def push_first(strg, loc, toks):
            expr_stack.append(toks[0])

        def push_u_minus(strg, loc, toks):
            if toks and toks[0] == '-':
                expr_stack.append('unary -')

        point = Literal('.')
        _e = CaselessLiteral('E')
        fnumber = Combine(Word('+-' + nums, nums) +
                          Optional(point + Optional(Word(nums))) +
                          Optional(_e + Word('+-' + nums, nums)))
        ident = Word(alphas, alphas + nums + '_$')

        plus = Literal("+")
        minus = Literal("-")
        mult = Literal("*")
        div = Literal("/")
        lpar = Literal("(").suppress()
        rpar = Literal(")").suppress()
        addop = plus | minus
        multop = mult | div
        expop = Literal("^")
        _pi = CaselessLiteral("PI")
        x = CaselessLiteral("X")

        expr = Forward()
        atom = (Optional("-") + (x | _pi | _e | fnumber | ident + lpar + expr + rpar).setParseAction(push_first) |
                (lpar + expr.suppress() + rpar)).setParseAction(push_u_minus)

        factor = Forward()
        factor << atom + ZeroOrMore((expop + factor).setParseAction(push_first))

        term = factor + ZeroOrMore((multop + factor).setParseAction(push_first))
        expr << term + ZeroOrMore((addop + term).setParseAction(push_first))

        expr.parseString(expression)
        return expr_stack


Instruction:
Can you generate a brief explanation for the following Python 3 code
def from_int(data):
    if not isinstance(data, int) and not isinstance(data, long):
        raise TypeError('Input must be integer')

    res = []
    while data > 0 or not res:
        for j in range(5):
            if not j % 2:
                res += CONSONANTS[(data & 0xf)]
                data >>= 4
            else:
                res += VOWELS[(data & 0x3)]
                data >>= 2
        if data > 0:
            res += '-'
    res.reverse()
    return ''.join(res)

Response: Convert an integer to a string in the format of a sequence of numbers.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def to_int(data):
    if not isinstance(data, basestring):
        raise TypeError('Input must be string')

    res = 0
    for part in data.split('-'):
        if len(part) != 5:
            raise ValueError('Malformed proquint')
        for j in range(5):
            try:
                if not j % 2:
                    res <<= 4
                    res |= CONSONANTS.index(part[j])
                else:
                    res <<= 2
                    res |= VOWELS.index(part[j])
            except ValueError:
                raise ValueError('Unknown character \'{!s}\' in proquint'.format(part[j]))
    return res

Response: decode a string into an integer


Instruction:
Create a Python 3 function to
get or create a shared key pair for certificate pushing.

Response:
def get_or_create_shared_key(cls, force_new=False):
        """
        Create a shared public/private key pair for certificate pushing,
        if the settings allow.
        """
        if force_new:
            with transaction.atomic():
                SharedKey.objects.filter(current=True).update(current=False)
                key = Key()
                return SharedKey.objects.create(public_key=key,
                                                private_key=key,
                                                current=True)
        # create a new shared key if one doesn't exist
        try:
            return SharedKey.objects.get(current=True)
        except SharedKey.DoesNotExist:
            key = Key()
            return SharedKey.objects.create(public_key=key,
                                            private_key=key,
                                            current=True)


Instruction:
Explain what the following Python 3 code does
def _self_referential_fk(klass_model):
    for f in klass_model._meta.concrete_fields:
        if f.related_model:
            if issubclass(klass_model, f.related_model):
                return f.attname
    return None

Response: Return whether this model has a self ref FK and the name for the field.


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

        # on Android, platform.platform() barfs, so we handle that safely here
        try:
            plat = platform.platform()
        except:
            plat = "Unknown (Android?)"

        kwargs = {
            "platform": plat,
            "hostname": platform.node(),
            "sysversion": sys.version,
            "database": DatabaseIDModel.get_or_create_current_database_id(),
            "db_path": os.path.abspath(settings.DATABASES['default']['NAME']),
            "system_id": os.environ.get("MORANGO_SYSTEM_ID", ""),
        }

        # try to get the MAC address, but exclude it if it was a fake (random) address
        mac = uuid.getnode()
        if (mac >> 40) % 2 == 0:  # 8th bit (of 48 bits, from left) is 1 if MAC is fake
            hashable_identifier = "{}:{}".format(kwargs['database'].id, mac)
            kwargs["node_id"] = hashlib.sha1(hashable_identifier.encode('utf-8')).hexdigest()[:20]
        else:
            kwargs["node_id"] = ""

        # do within transaction so we only ever have 1 current instance ID
        with transaction.atomic():
            InstanceIDModel.objects.filter(current=True).update(current=False)
            obj, created = InstanceIDModel.objects.get_or_create(**kwargs)
            obj.current = True
            obj.save()

        return obj, created

Response: Get the instance model corresponding to the current system or create a new one if it does not exist.


Instruction:
Can you tell what is the following Python 3 function doing
def serialize(self):
        # NOTE: code adapted from https://github.com/django/django/blob/master/django/forms/models.py#L75
        opts = self._meta

        data = {}
        for f in opts.concrete_fields:
            if f.attname in self.morango_fields_not_to_serialize:
                continue
            if f.attname in self._morango_internal_fields_not_to_serialize:
                continue
            # case if model is morango mptt
            if f.attname in getattr(self, '_internal_mptt_fields_not_to_serialize', '_internal_fields_not_to_serialize'):
                continue
            if hasattr(f, 'value_from_object_json_compatible'):
                data[f.attname] = f.value_from_object_json_compatible(self)
            else:
                data[f.attname] = f.value_from_object(self)
        return data

Response: Returns a dict of all concrete fields of the SyncableModel subclass except for those specifically blacklisted are returned in a dict.


Instruction:
Make a summary of the following Python 3 code
def deserialize(cls, dict_model):
        kwargs = {}
        for f in cls._meta.concrete_fields:
            if f.attname in dict_model:
                kwargs[f.attname] = dict_model[f.attname]
        return cls(**kwargs)

Response: Returns an unsaved class object based on the valid properties passed in.


Instruction:
Can you generate the documentation for the following Python 3 function
def get_default(self):
        if self.has_default():
            if callable(self.default):
                default = self.default()
                if isinstance(default, uuid.UUID):
                    return default.hex
                return default
            if isinstance(self.default, uuid.UUID):
                return self.default.hex
            return self.default
        return None

Response: Returns the default value for this field.


Instruction:
Here you have a function in Python 3, explain what it does
def add_to_deleted_models(sender, instance=None, *args, **kwargs):
    if issubclass(sender, SyncableModel):
        instance._update_deleted_models()

Response: Add a model to the list of deleted models.


Instruction:
Create a Python 3 function for
making a request to the specified URL and returns the response as json object.

Response:
def make_request(self, url, method='get', headers=None, data=None,
                     callback=None, errors=STRICT, verify=False, timeout=None, **params):
        """
        Reusable method for performing requests.
        :param url - URL to request
        :param method - request method, default is 'get'
        :param headers - request headers
        :param data - post data
        :param callback - callback to be applied to response,
                          default callback will parse response as json object.
        :param errors - specifies communication errors handling mode, possible
                        values are:
                         * strict (default) - throw an error as soon as one
                            occurred
                         * graceful - ignore certain errors, e.g. EmptyResponse
                         * ignore - ignore all errors and return a result in
                                    any case.
                                    NOTE that it DOES NOT mean that no
                                    exceptions can be
                                    raised from this method, it mostly ignores
                                    communication
                                    related errors.
                         * None or empty string equals to default
        :param verify - whether or not to verify SSL cert, default to False
        :param timeout - the timeout of the request in second, default to None
        :param params - additional query parameters for request
        """
        error_modes = (STRICT, GRACEFUL, IGNORE)
        error_mode = errors or GRACEFUL
        if error_mode.lower() not in error_modes:
            raise ValueError(
                'Possible values for errors argument are: %s'
                % ','.join(error_modes))

        if callback is None:
            callback = self._default_resp_callback

        request = getattr(requests, method.lower())
        log.debug('* Request URL: %s' % url)
        log.debug('* Request method: %s' % method)
        log.debug('* Request query params: %s' % params)
        log.debug('* Request headers: %s' % headers)
        log.debug('* Request timeout: %s' % timeout)

        r = request(
            url, headers=headers, data=data, verify=verify, timeout=timeout, params=params)

        log.debug('* r.url: %s' % r.url)

        try:
            r.raise_for_status()
            return callback(r)
        except Exception as e:
            return self._with_error_handling(r, e,
                                             error_mode, self.response_format)


Instruction:
Here you have a function in Python 3, explain what it does
def poll(self, url, initial_delay=2, delay=1, tries=20, errors=STRICT, is_complete_callback=None, **params):
        time.sleep(initial_delay)
        poll_response = None

        if is_complete_callback == None:
            is_complete_callback = self._default_poll_callback

        for n in range(tries):
            poll_response = self.make_request(url, headers=self._headers(),
                                              errors=errors, **params)

            if is_complete_callback(poll_response):
                return poll_response
            else:
                time.sleep(delay)

        if STRICT == errors:
            raise ExceededRetries(
                "Failed to poll within {0} tries.".format(tries))
        else:
            return poll_response

Response: Poll the URL url and return the response.


Instruction:
Write a Python 3 script to
check the condition in poll response to determine if it is complete and no subsequent poll requests should be done.

Response:
def _default_poll_callback(self, poll_resp):
        """
        Checks the condition in poll response to determine if it is complete
        and no subsequent poll requests should be done.
        """
        if poll_resp.parsed is None:
            return False
        success_list = ['UpdatesComplete', True, 'COMPLETE']
        status = None
        if self.response_format == 'xml':
            status = poll_resp.parsed.find('./Status').text
        elif self.response_format == 'json':
            status = poll_resp.parsed.get(
                'Status', poll_resp.parsed.get('status'))
        if status is None:
            raise RuntimeError('Unable to get poll response status.')
        return status in success_list


Instruction:
How would you explain what the following Python 3 function does
def _fsic_queuing_calc(fsic1, fsic2):
    return {instance: fsic2.get(instance, 0) for instance, counter in six.iteritems(fsic1) if fsic2.get(instance, 0) < counter}

Response: Calculate the lower counter between two fsics.


Instruction:
Explain what the following Python 3 code does
def _serialize_into_store(profile, filter=None):
    # ensure that we write and retrieve the counter in one go for consistency
    current_id = InstanceIDModel.get_current_instance_and_increment_counter()

    with transaction.atomic():
        # create Q objects for filtering by prefixes
        prefix_condition = None
        if filter:
            prefix_condition = functools.reduce(lambda x, y: x | y, [Q(_morango_partition__startswith=prefix) for prefix in filter])

        # filter through all models with the dirty bit turned on
        syncable_dict = _profile_models[profile]
        for (_, klass_model) in six.iteritems(syncable_dict):
            new_store_records = []
            new_rmc_records = []
            klass_queryset = klass_model.objects.filter(_morango_dirty_bit=True)
            if prefix_condition:
                klass_queryset = klass_queryset.filter(prefix_condition)
            store_records_dict = Store.objects.in_bulk(id_list=klass_queryset.values_list('id', flat=True))
            for app_model in klass_queryset:
                try:
                    store_model = store_records_dict[app_model.id]

                    # if store record dirty and app record dirty, append store serialized to conflicting data
                    if store_model.dirty_bit:
                        store_model.conflicting_serialized_data = store_model.serialized + "\n" + store_model.conflicting_serialized_data
                        store_model.dirty_bit = False

                    # set new serialized data on this store model
                    ser_dict = json.loads(store_model.serialized)
                    ser_dict.update(app_model.serialize())
                    store_model.serialized = DjangoJSONEncoder().encode(ser_dict)

                    # create or update instance and counter on the record max counter for this store model
                    RecordMaxCounter.objects.update_or_create(defaults={'counter': current_id.counter},
                                                              instance_id=current_id.id,
                                                              store_model_id=store_model.id)

                    # update last saved bys for this store model
                    store_model.last_saved_instance = current_id.id
                    store_model.last_saved_counter = current_id.counter
                    # update deleted flags in case it was previously deleted
                    store_model.deleted = False
                    store_model.hard_deleted = False

                    # update this model
                    store_model.save()

                except KeyError:
                    kwargs = {
                        'id': app_model.id,
                        'serialized': DjangoJSONEncoder().encode(app_model.serialize()),
                        'last_saved_instance': current_id.id,
                        'last_saved_counter': current_id.counter,
                        'model_name': app_model.morango_model_name,
                        'profile': app_model.morango_profile,
                        'partition': app_model._morango_partition,
                        'source_id': app_model._morango_source_id,
                    }
                    # check if model has FK pointing to it and add the value to a field on the store
                    self_ref_fk = _self_referential_fk(klass_model)
                    if self_ref_fk:
                        self_ref_fk_value = getattr(app_model, self_ref_fk)
                        kwargs.update({'_self_ref_fk': self_ref_fk_value or ''})
                    # create store model and record max counter for the app model
                    new_store_records.append(Store(**kwargs))
                    new_rmc_records.append(RecordMaxCounter(store_model_id=app_model.id, instance_id=current_id.id, counter=current_id.counter))

            # bulk create store and rmc records for this class
            Store.objects.bulk_create(new_store_records)
            RecordMaxCounter.objects.bulk_create(new_rmc_records)

            # set dirty bit to false for all instances of this model
            klass_queryset.update(update_dirty_bit_to=False)

        # get list of ids of deleted models
        deleted_ids = DeletedModels.objects.filter(profile=profile).values_list('id', flat=True)
        # update last_saved_bys and deleted flag of all deleted store model instances
        deleted_store_records = Store.objects.filter(id__in=deleted_ids)
        deleted_store_records.update(dirty_bit=False, deleted=True, last_saved_instance=current_id.id, last_saved_counter=current_id.counter)
        # update rmcs counters for deleted models that have our instance id
        RecordMaxCounter.objects.filter(instance_id=current_id.id, store_model_id__in=deleted_ids).update(counter=current_id.counter)
        # get a list of deleted model ids that don't have an rmc for our instance id
        new_rmc_ids = deleted_store_records.exclude(recordmaxcounter__instance_id=current_id.id).values_list("id", flat=True)
        # bulk create these new rmcs
        RecordMaxCounter.objects.bulk_create([RecordMaxCounter(store_model_id=r_id, instance_id=current_id.id, counter=current_id.counter) for r_id in new_rmc_ids])
        # clear deleted models table for this profile
        DeletedModels.objects.filter(profile=profile).delete()

        # handle logic for hard deletion models
        hard_deleted_ids = HardDeletedModels.objects.filter(profile=profile).values_list('id', flat=True)
        hard_deleted_store_records = Store.objects.filter(id__in=hard_deleted_ids)
        hard_deleted_store_records.update(hard_deleted=True, serialized='{}', conflicting_serialized_data='')
        HardDeletedModels.objects.filter(profile=profile).delete()

        # update our own database max counters after serialization
        if not filter:
            DatabaseMaxCounter.objects.update_or_create(instance_id=current_id.id, partition="", defaults={'counter': current_id.counter})
        else:
            for f in filter:
                DatabaseMaxCounter.objects.update_or_create(instance_id=current_id.id, partition=f, defaults={'counter': current_id.counter})

Response: Takes data from app layer and serializes the models into the store.


Instruction:
Explain what the following Python 3 code does
def _deserialize_from_store(profile):
    # we first serialize to avoid deserialization merge conflicts
    _serialize_into_store(profile)

    fk_cache = {}
    with transaction.atomic():
        syncable_dict = _profile_models[profile]
        excluded_list = []
        # iterate through classes which are in foreign key dependency order
        for model_name, klass_model in six.iteritems(syncable_dict):
            # handle cases where a class has a single FK reference to itself
            self_ref_fk = _self_referential_fk(klass_model)
            query = Q(model_name=klass_model.morango_model_name)
            for klass in klass_model.morango_model_dependencies:
                query |= Q(model_name=klass.morango_model_name)
            if self_ref_fk:
                clean_parents = Store.objects.filter(dirty_bit=False, profile=profile).filter(query).char_ids_list()
                dirty_children = Store.objects.filter(dirty_bit=True, profile=profile) \
                                              .filter(Q(_self_ref_fk__in=clean_parents) | Q(_self_ref_fk='')).filter(query)

                # keep iterating until size of dirty_children is 0
                while len(dirty_children) > 0:
                    for store_model in dirty_children:
                        try:
                            app_model = store_model._deserialize_store_model(fk_cache)
                            if app_model:
                                with mute_signals(signals.pre_save, signals.post_save):
                                    app_model.save(update_dirty_bit_to=False)
                            # we update a store model after we have deserialized it to be able to mark it as a clean parent
                            store_model.dirty_bit = False
                            store_model.save(update_fields=['dirty_bit'])
                        except exceptions.ValidationError:
                            # if the app model did not validate, we leave the store dirty bit set
                            excluded_list.append(store_model.id)

                    # update lists with new clean parents and dirty children
                    clean_parents = Store.objects.filter(dirty_bit=False, profile=profile).filter(query).char_ids_list()
                    dirty_children = Store.objects.filter(dirty_bit=True, profile=profile, _self_ref_fk__in=clean_parents).filter(query)
            else:
                # array for holding db values from the fields of each model for this class
                db_values = []
                fields = klass_model._meta.fields
                for store_model in Store.objects.filter(model_name=model_name, profile=profile, dirty_bit=True):
                    try:
                        app_model = store_model._deserialize_store_model(fk_cache)
                        # if the model was not deleted add its field values to the list
                        if app_model:
                            for f in fields:
                                value = getattr(app_model, f.attname)
                                db_value = f.get_db_prep_value(value, connection)
                                db_values.append(db_value)
                    except exceptions.ValidationError:
                        # if the app model did not validate, we leave the store dirty bit set
                        excluded_list.append(store_model.id)

                if db_values:
                    # number of rows to update
                    num_of_rows = len(db_values) // len(fields)
                    # create '%s' placeholders for a single row
                    placeholder_tuple = tuple(['%s' for _ in range(len(fields))])
                    # create list of the '%s' tuple placeholders based on number of rows to update
                    placeholder_list = [str(placeholder_tuple) for _ in range(num_of_rows)]
                    with connection.cursor() as cursor:
                        DBBackend._bulk_insert_into_app_models(cursor, klass_model._meta.db_table, fields, db_values, placeholder_list)

        # clear dirty bit for all store models for this profile except for models that did not validate
        Store.objects.exclude(id__in=excluded_list).filter(profile=profile, dirty_bit=True).update(dirty_bit=False)

Response: Deserializes data from the store into the application.