Instruction:
Can you implement a function in Python 3 that
searches the table for the entry specified by name and value.

Response:
def search(self, name, value):
        """
        Searches the table for the entry specified by name
        and value

        Returns one of the following:
            - ``None``, no match at all
            - ``(index, name, None)`` for partial matches on name only.
            - ``(index, name, value)`` for perfect matches.
        """
        partial = None

        header_name_search_result = CocaineHeaders.STATIC_TABLE_MAPPING.get(name)
        if header_name_search_result:
            index = header_name_search_result[1].get(value)
            if index is not None:
                return index, name, value
            partial = (header_name_search_result[0], name, None)

        offset = len(CocaineHeaders.STATIC_TABLE)
        for (i, (n, v)) in enumerate(self.dynamic_entries):
            if n == name:
                if v == value:
                    return i + offset + 1, n, v
                elif partial is None:
                    partial = (i + offset + 1, n, None)
        return partial


Instruction:
Can you create a Python 3 function that
shrinks the dynamic table to be at or below maxsize

Response:
def _shrink(self):
        """
        Shrinks the dynamic table to be at or below maxsize
        """
        cursize = self._current_size
        while cursize > self._maxsize:
            name, value = self.dynamic_entries.pop()
            cursize -= table_entry_size(name, value)
        self._current_size = cursize


Instruction:
How would you code a function in Python 3 to
add a new value for the given key.

Response:
def add(self, name, value):
        # type: (str, str) -> None
        """Adds a new value for the given key."""
        self._last_key = name
        if name in self:
            self._dict[name] = value
            self._as_list[name].append(value)
        else:
            self[name] = value


Instruction:
Write a Python 3 function that can
return an iterable of all ( name value ) pairs.

Response:
def get_all(self):
        # type: () -> typing.Iterable[typing.Tuple[str, str]]
        """Returns an iterable of all (name, value) pairs.

        If a header has multiple values, multiple pairs will be
        returned with the same name.
        """
        for name, values in six.iteritems(self._as_list):
            for value in values:
                yield (name, value)


Instruction:
Create a Python 3 function to
create the view used to edit permissions of the object.

Response:
def edit_permissions(self):
        """Creates the view used to edit permissions.

        To create the view, data in the following format is passed to the UI
        in the objects field:

        .. code-block:: python
            {
                "type": "tree-toggle",
                "action": "set_permission",
                "tree": [
                    {
                        "checked": true,
                        "name": "Workflow 1 Name",
                        "id": "workflow1",
                        "children":
                            [
                                {
                                    "checked": true,
                                    "name": "Task 1 Name",
                                    "id": "workflow1..task1",
                                    "children": []
                                },
                                {
                                    "checked": false,
                                    "id": "workflow1..task2",
                                    "name": "Task 2 Name",
                                    "children": []
                                }
                            ]
                    },
                    {
                        "checked": true,
                        "name": "Workflow 2 Name",
                        "id": "workflow2",
                        "children": [
                            {
                                "checked": true,
                                "name": "Workflow 2 Lane 1 Name",
                                "id": "workflow2.lane1",
                                "children": [
                                    {
                                        "checked": true,
                                        "name": "Workflow 2 Task 1 Name",
                                        "id": "workflow2.lane1.task1",
                                        "children": []
                                    },
                                    {
                                        "checked": false,
                                        "name": "Workflow 2 Task 2 Name",
                                        "id": "workflow2.lane1.task2",
                                        "children": []
                                    }
                                ]
                            }
                        ]
                    }
                ]
            }

        "type" field denotes that the object is a tree view which has elements that can be toggled.
        "action" field is the

        "name" field is the human readable name.
        "id" field is used to make requests to the backend.
        "checked" field shows whether the role has the permission or not.
        "children" field is the sub-permissions of the permission.
        """
        # Get the role that was selected in the CRUD view
        key = self.current.input['object_id']
        self.current.task_data['role_id'] = key
        role = RoleModel.objects.get(key=key)
        # Get the cached permission tree, or build a new one if there is none cached
        # TODO: Add an extra view in case there was no cache, as in 'please wait calculating permissions'
        permission_tree = self._permission_trees(PermissionModel.objects)
        # Apply the selected role to the permission tree, setting the 'checked' field
        # of the permission the role has
        role_tree = self._apply_role_tree(permission_tree, role)
        # Apply final formatting, and output the tree to the UI
        self.output['objects'] = [
            {
                'type': 'tree-toggle',
                'action': 'apply_change',
                'trees': self._format_tree_output(role_tree),
            },
        ]
        self.form_out(PermissionForm())


Instruction:
How would you explain what the following Python 3 function does
def _permission_trees(permissions):
        treecache = PermissionTreeCache()
        cached = treecache.get()
        if not cached:
            tree = PermissionTreeBuilder()
            for permission in permissions:
                tree.insert(permission)
            result = tree.serialize()
            treecache.set(result)
            return result
        return cached

Response: Get the cached permission tree or build a new one if necessary.


Instruction:
Write a Python 3 function for
traversing the permission tree returning the permission at given permission path.

Response:
def _traverse_tree(tree, path):
        """Traverses the permission tree, returning the permission at given permission path."""
        path_steps = (step for step in path.split('.') if step != '')
        # Special handling for first step, because the first step isn't under 'objects'
        first_step = path_steps.next()
        subtree = tree[first_step]

        for step in path_steps:
            subtree = subtree['children'][step]
        return subtree


Instruction:
Make a summary of the following Python 3 code
def apply_change(self):
        changes = self.input['change']
        key = self.current.task_data['role_id']
        role = RoleModel.objects.get(key=key)
        for change in changes:
            permission = PermissionModel.objects.get(code=change['id'])
            if change['checked'] is True:
                role.add_permission(permission)
            else:
                role.remove_permission(permission)
        role.save()

Response: Applies changes to the permissions of the role.


Instruction:
Given the following Python 3 function, write the documentation
def seek(self, offset):
        if self._shifts:
            if 0 <= offset < len(self._shifts):
                current_pos = self._file.tell()
                new_pos = self._shifts[offset]
                if current_pos != new_pos:
                    if current_pos == self._shifts[-1]:  # reached the end of the file
                        self._data = self.__reader()
                        self.__file = iter(self._file.readline, '')
                    self._file.seek(new_pos)
            else:
                raise IndexError('invalid offset')
        else:
            raise self._implement_error

Response: Seeks to a given number of record in the original file.


Instruction:
How would you code a function in Python 3 to
tell the file where the record was processed

Response:
def tell(self):
        """
        :return: number of records processed from the original file
        """
        if self._shifts:
            t = self._file.tell()
            return bisect_left(self._shifts, t)
        raise self._implement_error


Instruction:
Can you generate a brief explanation for the following Python 3 code
def write(self, data):
        m = self._convert_structure(data)
        self._file.write(self._format_mol(*m))
        self._file.write('M  END\n')

        for k, v in data.meta.items():
            self._file.write(f'>  <{k}>\n{v}\n')
        self._file.write('$$$$\n')

Response: write single molecule into file


Instruction:
How would you explain what the following Python 3 function does
def save_workflow_to_cache(self, serialized_wf_instance):
        # self.current.task_data['flow'] = None
        task_data = self.current.task_data.copy()
        for k, v in list(task_data.items()):
            if k.startswith('_'):
                del task_data[k]
        if 'cmd' in task_data:
            del task_data['cmd']

        self.wf_state.update({'step': serialized_wf_instance,
                              'data': task_data,
                              'name': self.current.workflow_name,
                              'wf_id': self.workflow_spec.wf_id
                              })

        if self.current.lane_id:
            self.current.pool[self.current.lane_id] = self.current.role.key
        self.wf_state['pool'] = self.current.pool
        self.current.log.debug("POOL Content before WF Save: %s" % self.current.pool)
        self.current.wf_cache.save(self.wf_state)

Response: Save the workflow instance to the cache.


Instruction:
How would you explain what the following Python 3 function does
def get_pool_context(self):
        # TODO: Add in-process caching
        """
        Builds context for the WF pool.

        Returns:
            Context dict.
        """
        context = {self.current.lane_id: self.current.role, 'self': self.current.role}
        for lane_id, role_id in self.current.pool.items():
            if role_id:
                context[lane_id] = lazy_object_proxy.Proxy(
                    lambda: self.role_model(super_context).objects.get(role_id))
        return context

Response: Builds a context dictionary for the WF pool.


Instruction:
Can you implement a function in Python 3 that
loads the serialized workflow state and data from cache and updates the self. current. task_data with the data from the cache.

Response:
def load_workflow_from_cache(self):
        """
        loads the serialized wf state and data from cache
        updates the self.current.task_data
        """
        if not self.current.new_token:
            self.wf_state = self.current.wf_cache.get(self.wf_state)
            self.current.task_data = self.wf_state['data']
            self.current.set_client_cmds()
            self.current.pool = self.wf_state['pool']
            return self.wf_state['step']


Instruction:
Implement a function in Python 3 to
serialize the current WF state data.

Response:
def serialize_workflow(self):
        """
        Serializes the current WF.

        Returns:
            WF state data.
        """
        self.workflow.refresh_waiting_tasks()
        return CompactWorkflowSerializer().serialize_workflow(self.workflow,
                                                              include_spec=False)


Instruction:
Can you create a Python 3 function that
tries to find the path of the BPMN diagram file . bpmn

Response:
def find_workflow_path(self):
        """
        Tries to find the path of the workflow diagram file
        in `WORKFLOW_PACKAGES_PATHS`.

        Returns:
            Path of the workflow spec file (BPMN diagram)
        """
        for pth in settings.WORKFLOW_PACKAGES_PATHS:
            path = "%s/%s.bpmn" % (pth, self.current.workflow_name)
            if os.path.exists(path):
                return path
        err_msg = "BPMN file cannot found: %s" % self.current.workflow_name
        log.error(err_msg)
        raise RuntimeError(err_msg)


Instruction:
Can you generate a brief explanation for the following Python 3 code
def get_worfklow_spec(self):
        # TODO: convert from in-process to redis based caching
        if self.current.workflow_name not in self.workflow_spec_cache:
            # path = self.find_workflow_path()
            # spec_package = InMemoryPackager.package_in_memory(self.current.workflow_name, path)
            # spec = BpmnSerializer().deserialize_workflow_spec(spec_package)

            try:
                self.current.wf_object = BPMNWorkflow.objects.get(name=self.current.workflow_name)
            except ObjectDoesNotExist:
                self.current.wf_object = BPMNWorkflow.objects.get(name='not_found')
                self.current.task_data['non-existent-wf'] = self.current.workflow_name
                self.current.workflow_name = 'not_found'
            xml_content = self.current.wf_object.xml.body
            spec = ZopsSerializer().deserialize_workflow_spec(xml_content, self.current.workflow_name)

            spec.wf_id = self.current.wf_object.key
            self.workflow_spec_cache[self.current.workflow_name] = spec
        return self.workflow_spec_cache[self.current.workflow_name]

Response: Generates and caches the workflow spec package from disk and returns the SpiffWorkflow Spec object.


Instruction:
Can you create a Python 3 function that
initializes the workflow with given request, response objects and diagram name. Args: session: input: workflow_name (str): Name of workflow diagram without ".bpmn" suffix. File must be placed under one of configured :py:attr:`~zengine.settings.WORKFLOW_PACKAGES_PATHS`

Response:
def start_engine(self, **kwargs):
        """
        Initializes the workflow with given request, response objects and diagram name.

        Args:
            session:
            input:
            workflow_name (str): Name of workflow diagram without ".bpmn" suffix.
             File must be placed under one of configured :py:attr:`~zengine.settings.WORKFLOW_PACKAGES_PATHS`

        """
        self.current = WFCurrent(**kwargs)
        self.wf_state = {'in_external': False, 'finished': False}
        if not self.current.new_token:
            self.wf_state = self.current.wf_cache.get(self.wf_state)
            self.current.workflow_name = self.wf_state['name']
            # if we have a pre-selected object to work with,
            # inserting it as current.input['id'] and task_data['object_id']
            if 'subject' in self.wf_state:
                self.current.input['id'] = self.wf_state['subject']
                self.current.task_data['object_id'] = self.wf_state['subject']
        self.check_for_authentication()
        self.check_for_permission()
        self.workflow = self.load_or_create_workflow()

        # if form data exists in input (user submitted)
        # put form data in wf task_data
        if 'form' in self.current.input:
            form = self.current.input['form']
            if 'form_name' in form:
                self.current.task_data[form['form_name']] = form

        # in wf diagram, if property is stated as init = True
        # demanded initial values are assigned and put to cache
        start_init_values = self.workflow_spec.wf_properties.get('init', 'False') == 'True'
        if start_init_values:
            WFInit = get_object_from_path(settings.WF_INITIAL_VALUES)()
            WFInit.assign_wf_initial_values(self.current)

        log_msg = ("\n\n::::::::::: ENGINE STARTED :::::::::::\n"
                   "\tWF: %s (Possible) TASK:%s\n"
                   "\tCMD:%s\n"
                   "\tSUBCMD:%s" % (
                       self.workflow.name,
                       self.workflow.get_tasks(Task.READY),
                       self.current.input.get('cmd'), self.current.input.get('subcmd')))
        log.debug(log_msg)
        sys._zops_wf_state_log = log_msg
        self.current.workflow = self.workflow


Instruction:
Can you tell what is the following Python 3 function doing
def generate_wf_state_log(self):
        output = '\n- - - - - -\n'
        output += "WORKFLOW: %s ( %s )" % (self.current.workflow_name.upper(),
                                           self.current.workflow.name)

        output += "\nTASK: %s ( %s )\n" % (self.current.task_name, self.current.task_type)
        output += "DATA:"
        for k, v in self.current.task_data.items():
            if v:
                output += "\n\t%s: %s" % (k, v)
        output += "\nCURRENT:"
        output += "\n\tACTIVITY: %s" % self.current.activity
        output += "\n\tPOOL: %s" % self.current.pool
        output += "\n\tIN EXTERNAL: %s" % self.wf_state['in_external']
        output += "\n\tLANE: %s" % self.current.lane_name
        output += "\n\tTOKEN: %s" % self.current.token
        sys._zops_wf_state_log = output
        return output

Response: Generates the log of the state of the workflow and content of the current task_data.


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

        """
        Main workflow switcher.

        This method recreates main workflow from `main wf` dict which
        was set by external workflow swicther previously.

        """

        # in external assigned as True in switch_to_external_wf.
        # external_wf should finish EndEvent and it's name should be
        # also EndEvent for switching again to main wf.
        if self.wf_state['in_external'] and self.current.task_type == 'EndEvent' and \
                self.current.task_name == 'EndEvent':

            # main_wf information was copied in switch_to_external_wf and it takes this information.
            main_wf = self.wf_state['main_wf']

            # main_wf_name is assigned to current workflow name again.
            self.current.workflow_name = main_wf['name']

            # For external WF, check permission and authentication. But after cleaning current task.
            self._clear_current_task()

            # check for auth and perm. current task cleared, do against new workflow_name
            self.check_for_authentication()
            self.check_for_permission()

            # WF knowledge is taken for main wf.
            self.workflow_spec = self.get_worfklow_spec()

            # WF instance is started again where leave off.
            self.workflow = self.deserialize_workflow(main_wf['step'])

            # Current WF is this WF instance.
            self.current.workflow = self.workflow

            # in_external is assigned as False
            self.wf_state['in_external'] = False

            # finished is assigned as False, because still in progress.
            self.wf_state['finished'] = False

            # pool info of main_wf is assigned.
            self.wf_state['pool'] = main_wf['pool']
            self.current.pool = self.wf_state['pool']

            # With main_wf is executed.
            self.run()

Response: This method recreates main workflow from external workflow dict which was set by switch_to_external_wf.


Instruction:
Write a Python 3 function for
switching to external WF.

Response:
def switch_to_external_wf(self):
        """
        External workflow switcher.

        This method copies main workflow information into
        a temporary dict `main_wf` and makes external workflow
        acting as main workflow.

        """

        # External WF name should be stated at main wf diagram and type should be service task.
        if (self.current.task_type == 'ServiceTask' and
                self.current.task.task_spec.type == 'external'):

            log.debug("Entering to EXTERNAL WF")

            # Main wf information is copied to main_wf.
            main_wf = self.wf_state.copy()

            # workflow name from main wf diagram is assigned to current workflow name.
            # workflow name must be either in task_data with key 'external_wf' or in main diagram's
            # topic.
            self.current.workflow_name = self.current.task_data.pop('external_wf', False) or self.\
                current.task.task_spec.topic

            # For external WF, check permission and authentication. But after cleaning current task.
            self._clear_current_task()

            # check for auth and perm. current task cleared, do against new workflow_name
            self.check_for_authentication()
            self.check_for_permission()

            # wf knowledge is taken for external wf.
            self.workflow_spec = self.get_worfklow_spec()
            # New WF instance is created for external wf.
            self.workflow = self.create_workflow()
            # Current WF is this WF instance.
            self.current.workflow = self.workflow
            # main_wf: main wf information.
            # in_external: it states external wf in progress.
            # finished: it shows that main wf didn't finish still progress in external wf.
            self.wf_state = {'main_wf': main_wf, 'in_external': True, 'finished': False}


Instruction:
Can you write a function in Python 3 where it
clears the current task related attributes

Response:
def _clear_current_task(self):

        """
        Clear tasks related attributes, checks permissions
        While switching WF to WF, authentication and permissions are checked for new WF.
        """
        self.current.task_name = None
        self.current.task_type = None
        self.current.task = None


Instruction:
Can you generate the documentation for the following Python 3 function
def check_for_rerun_user_task(self):
        data = self.current.input
        if 'wf_meta' in data:
            return

        current_task = self.workflow.get_tasks(Task.READY)[0]
        current_task_type = current_task.task_spec.__class__.__name__
        pre_task = current_task.parent
        pre_task_type = pre_task.task_spec.__class__.__name__

        if pre_task_type != 'UserTask':
            return

        if current_task_type == 'EndEvent':
            return

        pre_lane = pre_task.task_spec.lane
        current_lane = current_task.task_spec.lane
        if pre_lane == current_lane:
            pre_task._set_state(Task.READY)
            current_task._set_state(Task.MAYBE)

Response: Checks that the user task needs to re - run.


Instruction:
Create a Python 3 function to
switch the language of the current user.

Response:
def switch_lang(self):
        """Switch to the language of the current user.

        If the current language is already the specified one, nothing will be done.
        """
        locale = self.current.locale
        translation.InstalledLocale.install_language(locale['locale_language'])
        translation.InstalledLocale.install_locale(locale['locale_datetime'], 'datetime')
        translation.InstalledLocale.install_locale(locale['locale_number'], 'number')


Instruction:
Create a Python 3 function for
triggering a lane_user_change signal if we switched to a new lane and we don t have a user_id

Response:
def catch_lane_change(self):
        """
        trigger a lane_user_change signal if we switched to a new lane
        and new lane's user is different from current one
        """
        if self.current.lane_name:
            if self.current.old_lane and self.current.lane_name != self.current.old_lane:
                # if lane_name not found in pool or it's user different from the current(old) user
                if (self.current.lane_id not in self.current.pool or
                            self.current.pool[self.current.lane_id] != self.current.user_id):
                    self.current.log.info("LANE CHANGE : %s >> %s" % (self.current.old_lane,
                                                                      self.current.lane_name))
                    if self.current.lane_auto_sendoff:
                        self.current.sendoff_current_user()
                    self.current.flow_enabled = False
                    if self.current.lane_auto_invite:
                        self.current.invite_other_parties(self._get_possible_lane_owners())
                    return True


Instruction:
Write a Python 3 script to
parse the client message that defined in the current workflow task s inputOutput extension and adds the appropriate message to the current workflow message.

Response:
def parse_workflow_messages(self):
        """
        Transmits client message that defined in
        a workflow task's inputOutput extension

       .. code-block:: xml

            <bpmn2:extensionElements>
            <camunda:inputOutput>
            <camunda:inputParameter name="client_message">
            <camunda:map>
              <camunda:entry key="title">Teşekkürler</camunda:entry>
              <camunda:entry key="body">İşlem Başarılı</camunda:entry>
              <camunda:entry key="type">info</camunda:entry>
            </camunda:map>
            </camunda:inputParameter>
            </camunda:inputOutput>
            </bpmn2:extensionElements>

        """
        if 'client_message' in self.current.spec.data:
            m = self.current.spec.data['client_message']
            self.current.msg_box(title=m.get('title'),
                                 msg=m.get('body'),
                                 typ=m.get('type', 'info'))


Instruction:
Create a Python 3 function to
run the method that referenced from current task

Response:
def run_activity(self):
        """
        runs the method that referenced from current task
        """
        activity = self.current.activity
        if activity:
            if activity not in self.wf_activities:
                self._load_activity(activity)
            self.current.log.debug(
                "Calling Activity %s from %s" % (activity, self.wf_activities[activity]))
            self.wf_activities[self.current.activity](self.current)


Instruction:
How would you explain what the following Python 3 function does
def _import_object(self, path, look_for_cls_method):
        last_nth = 2 if look_for_cls_method else 1
        path = path.split('.')
        module_path = '.'.join(path[:-last_nth])
        class_name = path[-last_nth]
        module = importlib.import_module(module_path)
        if look_for_cls_method and path[-last_nth:][0] == path[-last_nth]:
            class_method = path[-last_nth:][1]
        else:
            class_method = None
        return getattr(module, class_name), class_name, class_method

Response: Imports the object from the module that contains the referenced method.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def _load_activity(self, activity):
        fpths = []
        full_path = ''
        errors = []
        paths = settings.ACTIVITY_MODULES_IMPORT_PATHS
        number_of_paths = len(paths)
        for index_no in range(number_of_paths):
            full_path = "%s.%s" % (paths[index_no], activity)
            for look4kls in (0, 1):
                try:
                    self.current.log.info("try to load from %s[%s]" % (full_path, look4kls))
                    kls, cls_name, cls_method = self._import_object(full_path, look4kls)
                    if cls_method:
                        self.current.log.info("WILLCall %s(current).%s()" % (kls, cls_method))
                        self.wf_activities[activity] = lambda crnt: getattr(kls(crnt), cls_method)()
                    else:
                        self.wf_activities[activity] = kls
                    return
                except (ImportError, AttributeError):
                    fpths.append(full_path)
                    errmsg = "{activity} not found under these paths:\n\n >>> {paths} \n\n" \
                             "Error Messages:\n {errors}"
                    errors.append("\n========================================================>\n"
                                  "| PATH | %s"
                                  "\n========================================================>\n\n"
                                  "%s" % (full_path, traceback.format_exc()))
                    assert index_no != number_of_paths - 1, errmsg.format(activity=activity,
                                                                          paths='\n >>> '.join(
                                                                              set(fpths)),
                                                                          errors='\n\n'.join(errors)
                                                                          )
                except:
                    self.current.log.exception("Cannot found the %s" % activity)

Response: Loads the given activity from the given path.


Instruction:
Can you tell what is the following Python 3 function doing
def check_for_authentication(self):
        auth_required = self.current.workflow_name not in settings.ANONYMOUS_WORKFLOWS
        if auth_required and not self.current.is_auth:
            self.current.log.debug("LOGIN REQUIRED:::: %s" % self.current.workflow_name)
            raise HTTPError(401, "Login required for %s" % self.current.workflow_name)

Response: Checks current workflow against the list of anonymous workflows.


Instruction:
Implement a Python 3 function for
checking if the user has permission to access a specific lane.

Response:
def check_for_lane_permission(self):
        """
        One or more permissions can be associated with a lane
        of a workflow. In a similar way, a lane can be
        restricted with relation to other lanes of the workflow.

        This method called on lane changes and checks user has
        required permissions and relations.

        Raises:
             HTTPForbidden: if the current user hasn't got the
              required permissions and proper relations

        """
        # TODO: Cache lane_data in app memory
        if self.current.lane_permission:
            log.debug("HAS LANE PERM: %s" % self.current.lane_permission)
            perm = self.current.lane_permission
            if not self.current.has_permission(perm):
                raise HTTPError(403, "You don't have required lane permission: %s" % perm)

        if self.current.lane_relations:
            context = self.get_pool_context()
            log.debug("HAS LANE RELS: %s" % self.current.lane_relations)
            try:
                cond_result = eval(self.current.lane_relations, context)
            except:
                log.exception("CONDITION EVAL ERROR : %s || %s" % (
                    self.current.lane_relations, context))
                raise
            if not cond_result:
                log.debug("LANE RELATION ERR: %s %s" % (self.current.lane_relations, context))
                raise HTTPError(403, "You aren't qualified for this lane: %s" %
                                self.current.lane_relations)


Instruction:
Write a Python 3 script for
checking if current user has the required permission for current workflow step.

Response:
def check_for_permission(self):
        # TODO: Works but not beautiful, needs review!
        """
        Checks if current user (or role) has the required permission
        for current workflow step.

        Raises:
            HTTPError: if user doesn't have required permissions.
        """
        if self.current.task:
            lane = self.current.lane_id
            permission = "%s.%s.%s" % (self.current.workflow_name, lane, self.current.task_name)
        else:
            permission = self.current.workflow_name
        log.debug("CHECK PERM: %s" % permission)

        if (self.current.task_type not in PERM_REQ_TASK_TYPES or
                permission.startswith(tuple(settings.ANONYMOUS_WORKFLOWS)) or
                (self.current.is_auth and permission.startswith(tuple(settings.COMMON_WORKFLOWS)))):
            return
        # FIXME:needs hardening

        log.debug("REQUIRE PERM: %s" % permission)
        if not self.current.has_permission(permission):
            raise HTTPError(403, "You don't have required permission: %s" % permission)


Instruction:
Can you generate a brief explanation for the following Python 3 code
def handle_wf_finalization(self):
        if ((not self.current.flow_enabled or (
            self.current.task_type.startswith('End') and not self.are_we_in_subprocess())) and
                    'token' in self.current.output):
            del self.current.output['token']

Response: Removes the token key from the current. output if WF is over.


Instruction:
Can you tell what is the following Python 3 function doing
def from_rdkit_molecule(data):
    m = MoleculeContainer()
    atoms, mapping = [], []
    for a in data.GetAtoms():
        atom = {'element': a.GetSymbol(), 'charge': a.GetFormalCharge()}
        atoms.append(atom)
        mapping.append(a.GetAtomMapNum())

        isotope = a.GetIsotope()
        if isotope:
            atom['isotope'] = isotope
        radical = a.GetNumRadicalElectrons()
        if radical:
            atom['multiplicity'] = radical + 1

    conformers = data.GetConformers()
    if conformers:
        for atom, (x, y, z) in zip(atoms, conformers[0].GetPositions()):
            atom['x'] = x
            atom['y'] = y
            atom['z'] = z

    for atom, mapping in zip(atoms, mapping):
        a = m.add_atom(atom)
        if mapping:
            m.atom(a)._parsed_mapping = mapping

    for bond in data.GetBonds():
        m.add_bond(bond.GetBeginAtomIdx() + 1, bond.GetEndAtomIdx() + 1, _rdkit_bond_map[bond.GetBondType()])

    return m

Response: Convert a RDKit molecule object to a MoleculeContainer object.


Instruction:
Here you have a function in Python 3, explain what it does
def to_rdkit_molecule(data):
    mol = RWMol()
    conf = Conformer()
    mapping = {}
    is_3d = False
    for n, a in data.atoms():
        ra = Atom(a.number)
        ra.SetAtomMapNum(n)
        if a.charge:
            ra.SetFormalCharge(a.charge)
        if a.isotope != a.common_isotope:
            ra.SetIsotope(a.isotope)
        if a.radical:
            ra.SetNumRadicalElectrons(a.radical)
        mapping[n] = m = mol.AddAtom(ra)
        conf.SetAtomPosition(m, (a.x, a.y, a.z))
        if a.z:
            is_3d = True
    if not is_3d:
        conf.Set3D(False)

    for n, m, b in data.bonds():
        mol.AddBond(mapping[n], mapping[m], _bond_map[b.order])

    mol.AddConformer(conf)
    SanitizeMol(mol)
    return mol

Response: Convert a MoleculeContainer to RDKit molecule object


Instruction:
Can you tell what is the following Python 3 function doing
def get_args_parser():
    parser = argparse.ArgumentParser(
        description='Marabunta: Migrating ants for Odoo')
    parser.add_argument('--migration-file', '-f',
                        action=EnvDefault,
                        envvar='MARABUNTA_MIGRATION_FILE',
                        required=True,
                        help='The yaml file containing the migration steps')
    parser.add_argument('--database', '-d',
                        action=EnvDefault,
                        envvar='MARABUNTA_DATABASE',
                        required=True,
                        help="Odoo's database")
    parser.add_argument('--db-user', '-u',
                        action=EnvDefault,
                        envvar='MARABUNTA_DB_USER',
                        required=True,
                        help="Odoo's database user")
    parser.add_argument('--db-password', '-w',
                        action=EnvDefault,
                        envvar='MARABUNTA_DB_PASSWORD',
                        required=True,
                        help="Odoo's database password")
    parser.add_argument('--db-port', '-p',
                        default=os.environ.get('MARABUNTA_DB_PORT', 5432),
                        help="Odoo's database port")
    parser.add_argument('--db-host', '-H',
                        default=os.environ.get('MARABUNTA_DB_HOST',
                                               'localhost'),
                        help="Odoo's database host")
    parser.add_argument('--mode',
                        action=EnvDefault,
                        envvar='MARABUNTA_MODE',
                        required=False,
                        help="Specify the mode in which we run the migration,"
                             "such as 'demo' or 'prod'. Additional operations "
                             "of this mode will be executed after the main "
                             "operations and the addons list of this mode "
                             "will be merged with the main addons list.")
    parser.add_argument('--allow-serie',
                        action=BoolEnvDefault,
                        required=False,
                        envvar='MARABUNTA_ALLOW_SERIE',
                        help='Allow to run more than 1 version upgrade at a '
                             'time.')
    parser.add_argument('--force-version',
                        required=False,
                        default=os.environ.get('MARABUNTA_FORCE_VERSION'),
                        help='Force upgrade of a version, even if it has '
                             'already been applied.')

    group = parser.add_argument_group(
        title='Web',
        description='Configuration related to the internal web server, '
                    'used to publish a maintenance page during the migration.',
    )
    group.add_argument('--web-host',
                       required=False,
                       default=os.environ.get('MARABUNTA_WEB_HOST', '0.0.0.0'),
                       help='Host for the web server')
    group.add_argument('--web-port',
                       required=False,
                       default=os.environ.get('MARABUNTA_WEB_PORT', 8069),
                       help='Port for the web server')
    group.add_argument('--web-custom-html',
                       required=False,
                       default=os.environ.get(
                           'MARABUNTA_WEB_CUSTOM_HTML'
                       ),
                       help='Path to a custom html file to publish')
    return parser

Response: Returns a parser for command line options.


Instruction:
Can you tell what is the following Python 3 function doing
def set_current(self, current):
        self.current = current
        self.input = current.input
        # self.req = current.request
        # self.resp = current.response
        self.output = current.output
        self.cmd = current.task_data['cmd']

        if self.cmd and NEXT_CMD_SPLITTER in self.cmd:
            self.cmd, self.next_cmd = self.cmd.split(NEXT_CMD_SPLITTER)
        else:
            self.next_cmd = None

Response: Sets the current attribute of the object.


Instruction:
Can you tell what is the following Python 3 function doing
def form_out(self, _form=None):
        _form = _form or self.object_form
        self.output['forms'] = _form.serialize()
        self._add_meta_props(_form)
        self.output['forms']['grouping'] = _form.Meta.grouping
        self.output['forms']['constraints'] = _form.Meta.constraints
        self._patch_form(self.output['forms'])
        self.set_client_cmd('form')

Response: Renders a single object form.


Instruction:
Write a Python 3 function for
adding given cmd to the internal list of client commands.

Response:
def set_client_cmd(self, *args):
        """
        Adds given cmd(s) to ``self.output['client_cmd']``

        Args:
            *args: Client commands.
        """
        self.client_cmd.update(args)
        self.output['client_cmd'] = list(self.client_cmd)


Instruction:
Here you have a function in Python 3, explain what it does
def run(self):
        from zengine.models import User
        user = User(username=self.manager.args.username, superuser=self.manager.args.super)
        user.set_password(self.manager.args.password)
        user.save()
        print("New user created with ID: %s" % user.key)

Response: Creates user encrypts password.


Instruction:
Can you generate the documentation for the following Python 3 function
def run(self):
        print("Development server started on http://%s:%s. \n\nPress Ctrl+C to stop\n" % (
            self.manager.args.addr,
            self.manager.args.port)
              )
        if self.manager.args.server_type == 'falcon':
            self.run_with_falcon()
        elif self.manager.args.server_type == 'tornado':
            self.run_with_tornado()

Response: Starts a development server for the zengine application.


Instruction:
Can you write a function in Python 3 where it
runs the tornado test server with tornado.

Response:
def run_with_tornado(self):
        """
        runs the tornado/websockets based test server
        """
        from zengine.tornado_server.server import runserver
        runserver(self.manager.args.addr, int(self.manager.args.port))


Instruction:
Create a Python 3 function to
run the falcon based test server

Response:
def run_with_falcon(self):
        """
        runs the falcon/http based test server
        """
        from wsgiref import simple_server
        from zengine.server import app
        httpd = simple_server.make_server(self.manager.args.addr, int(self.manager.args.port), app)
        httpd.serve_forever()


Instruction:
Can you implement a function in Python 3 that
starts a development server for the zengine application.

Response:
def run(self):
        """
        Starts a development server for the zengine application
        """
        from zengine.wf_daemon import run_workers, Worker

        worker_count = int(self.manager.args.workers or 1)
        if not self.manager.args.daemonize:
            print("Starting worker(s)")

        if worker_count > 1 or self.manager.args.autoreload:
            run_workers(worker_count,
                        self.manager.args.paths.split(' '),
                        self.manager.args.daemonize)
        else:
            worker = Worker()
            worker.run()


Instruction:
Given the following Python 3 function, write the documentation
def _prepare_domain(mapping):
        # Parse the domain-directory mapping
        try:
            domain, dir = mapping.split(':')
        except ValueError:
            print("Please provide the sources in the form of '<domain>:<directory>'")
            sys.exit(1)

        try:
            default_language = settings.TRANSLATION_DOMAINS[domain]
        except KeyError:
            print("Unknown domain {domain}, check the settings file to make sure"
                  " this domain is set in TRANSLATION_DOMAINS".format(domain=domain))
            sys.exit(1)
        # Create a temporary file to hold the `.pot` file for this domain
        handle, path = tempfile.mkstemp(prefix='zengine_i18n_', suffix='.pot')
        return (domain, {
            'default': default_language,
            'pot': path,
            'source': dir,
        })

Response: Prepare a helper dictionary for the domain to temporarily hold some information.


Instruction:
Write a Python 3 function that can
check that all domains specified in the settings were provided in the options.

Response:
def _validate_domains(domains):
        """Check that all domains specified in the settings was provided in the options."""
        missing = set(settings.TRANSLATION_DOMAINS.keys()) - set(domains.keys())
        if missing:
            print('The following domains have been set in the configuration, '
                  'but their sources were not provided, use the `--source` '
                  'option to specify their sources: {domains}'.format(domains=', '.join(missing)))
            sys.exit(1)


Instruction:
Create a Python 3 function for
extracting the translations into. pot files for the specified domains.

Response:
def _extract_translations(self, domains):
        """Extract the translations into `.pot` files"""
        for domain, options in domains.items():
            # Create the extractor
            extractor = babel_frontend.extract_messages()
            extractor.initialize_options()
            # The temporary location to write the `.pot` file
            extractor.output_file = options['pot']
            # Add the comments marked with 'tn:' to the translation file for translators to read. Strip the marker.
            extractor.add_comments = ['tn:']
            extractor.strip_comments = True
            # The directory where the sources for this domain are located
            extractor.input_paths = [options['source']]
            # Pass the metadata to the translator
            extractor.msgid_bugs_address = self.manager.args.contact
            extractor.copyright_holder = self.manager.args.copyright
            extractor.version = self.manager.args.version
            extractor.project = self.manager.args.project
            extractor.finalize_options()
            # Add keywords for lazy translation functions, based on their non-lazy variants
            extractor.keywords.update({
                'gettext_lazy': extractor.keywords['gettext'],
                'ngettext_lazy': extractor.keywords['ngettext'],
                '__': extractor.keywords['gettext'],  # double underscore for lazy
            })
            # Do the extraction
            _run_babel_command(extractor)


Instruction:
How would you explain what the following Python 3 function does
def _init_update_po_files(self, domains):
        for language in settings.TRANSLATIONS:
            for domain, options in domains.items():
                if language == options['default']: continue  # Default language of the domain doesn't need translations
                if os.path.isfile(_po_path(language, domain)):
                    # If the translation already exists, update it, keeping the parts already translated
                    self._update_po_file(language, domain, options['pot'])
                else:
                    # The translation doesn't exist, create a new translation file
                    self._init_po_file(language, domain, options['pot'])

Response: Update or initialize the. po files


Instruction:
Given the following Python 3 function, write the documentation
def _cleanup(self, domains):
        for option in domains.values():
            try:
                os.remove(option['pot'])
            except (IOError, OSError):
                # It is not a problem if we can't actually remove the temporary file
                pass

Response: Remove the temporary. pot files that were created for the domains.


Instruction:
How would you explain what the following Python 3 function does
def run(self):
        from zengine.lib.cache import WFSpecNames

        if self.manager.args.clear:
            self._clear_models()
            return

        if self.manager.args.wf_path:
            paths = self.get_wf_from_path(self.manager.args.wf_path)
        else:
            paths = self.get_workflows()

        self.count = 0

        self.do_with_submit(self.load_diagram, paths, threads=self.manager.args.threads)

        WFSpecNames().refresh()

        print("%s BPMN file loaded" % self.count)

Response: reads workflows checks if it s updated tries to update if there aren t any running instances of that wf


Instruction:
Explain what the following Python 3 code does
def get_wf_from_path(self, path):
        with open(path) as fp:
            content = fp.read()
        return [(os.path.basename(os.path.splitext(path)[0]), content), ]

Response: Load xml from given path


Instruction:
Can you implement a function in Python 3 that
yields XML content of all workflows found under WORKFLOW_PACKAGES_PATHS

Response:
def get_workflows(self):
        """
        Scans and loads all wf found under WORKFLOW_PACKAGES_PATHS

        Yields: XML content of diagram file

        """
        for pth in settings.WORKFLOW_PACKAGES_PATHS:
            for f in glob.glob("%s/*.bpmn" % pth):
                with open(f) as fp:
                    yield os.path.basename(os.path.splitext(f)[0]), fp.read()


Instruction:
Explain what the following Python 3 code does
def check_migration_and_solr(self):
        from pyoko.db.schema_update import SchemaUpdater
        from socket import error as socket_error
        from pyoko.conf import settings
        from importlib import import_module

        import_module(settings.MODELS_MODULE)
        registry = import_module('pyoko.model').model_registry
        models = [model for model in registry.get_base_models()]
        try:
            print(__(u"Checking migration and solr ..."))
            updater = SchemaUpdater(models, 1, False)
            updater.run(check_only=True)

        except socket_error as e:
            print(__(u"{0}Error not connected, open redis and rabbitmq{1}").format(CheckList.FAIL,
                                                                                CheckList.ENDC))

Response: Check migration and solr


Instruction:
Can you write a function in Python 3 where it
checks the connection to the Redis server

Response:
def check_redis():
        """
            Redis checks the connection
            It displays on the screen whether or not you have a connection.
        """
        from pyoko.db.connection import cache
        from redis.exceptions import ConnectionError

        try:
            cache.ping()
            print(CheckList.OKGREEN + "{0}Redis is working{1}" + CheckList.ENDC)
        except ConnectionError as e:
            print(__(u"{0}Redis is not working{1} ").format(CheckList.FAIL,
                                                            CheckList.ENDC), e.message)


Instruction:
Implement a Python 3 function for
checking the RabbitMQ connection and displays the connection details.

Response:
def check_mq_connection(self):
        """
        RabbitMQ checks the connection
        It displays on the screen whether or not you have a connection.
        """
        import pika
        from zengine.client_queue import BLOCKING_MQ_PARAMS
        from pika.exceptions import ProbableAuthenticationError, ConnectionClosed

        try:
            connection = pika.BlockingConnection(BLOCKING_MQ_PARAMS)
            channel = connection.channel()
            if channel.is_open:
                print(__(u"{0}RabbitMQ is working{1}").format(CheckList.OKGREEN, CheckList.ENDC))
            elif self.channel.is_closed or self.channel.is_closing:
                print(__(u"{0}RabbitMQ is not working!{1}").format(CheckList.FAIL, CheckList.ENDC))
        except ConnectionClosed as e:
            print(__(u"{0}RabbitMQ is not working!{1}").format(CheckList.FAIL, CheckList.ENDC), e)
        except ProbableAuthenticationError as e:
            print(__(u"{0}RabbitMQ username and password wrong{1}").format(CheckList.FAIL,
                                                                           CheckList.ENDC))


Instruction:
How would you code a function in Python 3 to
check if the environment variables are correct and if they are using the correct variables.

Response:
def check_encoding_and_env():
        """
        It brings the environment variables to the screen.
        The user checks to see if they are using the correct variables.
        """
        import sys
        import os
        if sys.getfilesystemencoding() in ['utf-8', 'UTF-8']:
            print(__(u"{0}File system encoding correct{1}").format(CheckList.OKGREEN,
                                                                   CheckList.ENDC))
        else:
            print(__(u"{0}File system encoding wrong!!{1}").format(CheckList.FAIL,
                                                                   CheckList.ENDC))
        check_env_list = ['RIAK_PROTOCOL', 'RIAK_SERVER', 'RIAK_PORT', 'REDIS_SERVER',
                          'DEFAULT_BUCKET_TYPE', 'PYOKO_SETTINGS',
                          'MQ_HOST', 'MQ_PORT', 'MQ_USER', 'MQ_VHOST',
                          ]
        env = os.environ
        for k, v in env.items():
            if k in check_env_list:
                print(__(u"{0}{1} : {2}{3}").format(CheckList.BOLD, k, v, CheckList.ENDC))


Instruction:
Implement a function in Python 3 to
find if the game is over.

Response:
def no_moves(position):
    """
    Finds if the game is over.

    :type: position: Board
    :rtype: bool
    """
    return position.no_moves(color.white) \
        or position.no_moves(color.black)


Instruction:
Can you generate a brief explanation for the following Python 3 code
def is_checkmate(position, input_color):
    return position.no_moves(input_color) and \
        position.get_king(input_color).in_check(position)

Response: Returns True if particular King is checkmated.


Instruction:
Can you create a Python 3 function that
returns a QuerySet object and a pagination data dict as a tuple containing the current page number and the number of objects per page.

Response:
def _paginate(self, current_page, query_set, per_page=10):
    """
    Handles pagination of object listings.

    Args:
        current_page int:
            Current page number
        query_set (:class:`QuerySet<pyoko:pyoko.db.queryset.QuerySet>`):
            Object listing queryset.
        per_page int:
            Objects per page.

    Returns:
        QuerySet object, pagination data dict as a tuple
    """
    total_objects = query_set.count()
    total_pages = int(total_objects / per_page or 1)
    # add orphans to last page
    current_per_page = per_page + (
        total_objects % per_page if current_page == total_pages else 0)
    pagination_data = dict(page=current_page,
                           total_pages=total_pages,
                           total_objects=total_objects,
                           per_page=current_per_page)
    query_set = query_set.set_params(rows=current_per_page, start=(current_page - 1) * per_page)
    return query_set, pagination_data


Instruction:
Write a Python 3 script to
create a new message for the given channel.

Response:
def create_message(current):
    """
    Creates a message for the given channel.

    .. code-block:: python

        # request:
        {
            'view':'_zops_create_message',
            'message': {
                'channel': key,     # of channel
                'body': string,     # message text.,
                'type': int,        # zengine.messaging.model.MSG_TYPES,
                'attachments': [{
                    'description': string,  # can be blank,
                    'name': string,         # file name with extension,
                    'content': string,      # base64 encoded file content
                    }]}
        # response:
            {
            'status': 'Created',
            'code': 201,
            'msg_key': key,     # key of the message object,
            }

    """
    msg = current.input['message']
    msg_obj = Channel.add_message(msg['channel'], body=msg['body'], typ=msg['type'],
                                  sender=current.user,
                                  title=msg['title'], receiver=msg['receiver'] or None)
    current.output = {
        'msg_key': msg_obj.key,
        'status': 'Created',
        'code': 201
    }
    if 'attachment' in msg:
        for atch in msg['attachments']:
            typ = current._dedect_file_type(atch['name'], atch['content'])
            Attachment(channel_id=msg['channel'], msg=msg_obj, name=atch['name'],
                       file=atch['content'], description=atch['description'], typ=typ).save()


Instruction:
Implement a function in Python 3 to
display the channel content.

Response:
def show_channel(current, waited=False):
    """
    Initial display of channel content.
    Returns channel description, members, no of members, last 20 messages etc.

    .. code-block:: python

        #  request:
            {
                'view':'_zops_show_channel',
                'key': key,
            }

        #  response:
            {
            'channel_key': key,
            'description': string,
            'no_of_members': int,
            'member_list': [
                {'name': string,
                 'is_online': bool,
                 'avatar_url': string,
                }],
            'name': string,
            'last_messages': [MSG_DICT]
            'status': 'OK',
            'code': 200
            }
    """
    ch = Channel(current).objects.get(current.input['key'])
    sbs = ch.get_subscription_for_user(current.user_id)
    current.output = {'key': current.input['key'],
                      'description': ch.description,
                      'name': sbs.name,
                      'actions': sbs.get_actions(),
                      'avatar_url': ch.get_avatar(current.user),
                      'no_of_members': len(ch.subscriber_set),
                      'member_list': [{'name': sb.user.full_name,
                                       'is_online': sb.user.is_online(),
                                       'avatar_url': sb.user.get_avatar_url()
                                       } for sb in ch.subscriber_set.objects.all()],
                      'last_messages': [],
                      'status': 'OK',
                      'code': 200
                      }
    for msg in ch.get_last_messages():
        current.output['last_messages'].insert(0, msg.serialize(current.user))


Instruction:
Can you tell what is the following Python 3 function doing
def channel_history(current):
    current.output = {
        'status': 'OK',
        'code': 201,
        'messages': []
    }

    for msg in list(Message.objects.filter(channel_id=current.input['channel_key'],
                                      updated_at__lte=current.input['timestamp'])[:20]):
        current.output['messages'].insert(0, msg.serialize(current.user))
    # FIXME: looks like  pyoko's __lt is broken
    # TODO: convert lte to lt and remove this block, when __lt filter fixed
    if current.output['messages']:
        current.output['messages'].pop(-1)

Response: Get old messages for a channel. 20 messages per request


Instruction:
How would you implement a function in Python 3 that
pushes timestamp of latest message of an ACTIVE channel. This view should be called with timestamp of latest message; - When user opens (clicks on) a channel. - Periodically (eg: setInterval for 15secs) while user staying in a channel. .. code-block:: python # request: { 'view':'_zops_last_seen_msg', 'channel_key': key, 'key': key, 'timestamp': datetime, } # response: { 'status': 'OK', 'code': 200, }

Response:
def report_last_seen_message(current):
    """
    Push timestamp of latest message of an ACTIVE channel.

    This view should be called with timestamp of latest message;
    - When user opens (clicks on) a channel.
    - Periodically (eg: setInterval for 15secs) while user staying in a channel.

    .. code-block:: python

        #  request:
            {
            'view':'_zops_last_seen_msg',
            'channel_key': key,
            'key': key,
            'timestamp': datetime,
            }

        #  response:
            {
            'status': 'OK',
            'code': 200,
            }
    """
    sbs = Subscriber(current).objects.filter(channel_id=current.input['channel_key'],
                                             user_id=current.user_id)[0]
    sbs.last_seen_msg_time = current.input['timestamp']
    sbs.save()
    current.output = {
        'status': 'OK',
        'code': 200}


Instruction:
How would you explain what the following Python 3 function does
def list_channels(current):
    current.output = {
        'status': 'OK',
        'code': 200,
        'channels': []}
    for sbs in current.user.subscriptions.objects.filter(is_visible=True):
        try:
            current.output['channels'].append(sbs.get_channel_listing())
        except ObjectDoesNotExist:
            # FIXME: This should not happen,
            log.exception("UNPAIRED DIRECT EXCHANGES!!!!")
            sbs.delete()

Response: List the channel memberships of the current user.


Instruction:
Can you generate the documentation for the following Python 3 function
def unread_count(current):
    unread_ntf = 0
    unread_msg = 0
    for sbs in current.user.subscriptions.objects.filter(is_visible=True):
        try:
            if sbs.channel.key == current.user.prv_exchange:
                unread_ntf += sbs.unread_count()
            else:
                unread_msg += sbs.unread_count()
        except ObjectDoesNotExist:
            # FIXME: This should not happen,
            log.exception("MULTIPLE PRV EXCHANGES!!!!")
            sbs.delete()
    current.output = {
        'status': 'OK',
        'code': 200,
        'notifications': unread_ntf,
        'messages': unread_msg
    }

Response: Unread count of all unread messages for current user


Instruction:
Make a summary of the following Python 3 code
def get_notifications(current):
    current.output = {
        'status': 'OK',
        'code': 200,
        'notifications': [],
    }
    amount = current.input.get('amount', 8)
    try:
        notif_sbs = current.user.subscriptions.objects.get(channel_id=current.user.prv_exchange)
    except MultipleObjectsReturned:
        # FIXME: This should not happen,
        log.exception("MULTIPLE PRV EXCHANGES!!!!")
        sbs = current.user.subscriptions.objects.filter(channel_id=current.user.prv_exchange)
        sbs[0].delete()
        notif_sbs = sbs[1]
    for msg in notif_sbs.channel.message_set.objects.all()[:amount]:
        current.output['notifications'].insert(0, {
            'title': msg.msg_title,
            'body': msg.body,
            'type': msg.typ,
            'url': msg.url,
            'channel_key': msg.channel.key,
            'message_key': msg.key,
            'timestamp': msg.updated_at})

Response: Returns last N notifications for current user


Instruction:
Explain what the following Python 3 code does
def create_channel(current):
    channel = Channel(name=current.input['name'],
                      description=current.input['description'],
                      owner=current.user,
                      typ=15).save()
    with BlockSave(Subscriber):
        Subscriber.objects.get_or_create(user=channel.owner,
                                         channel=channel,
                                         can_manage=True,
                                         can_leave=False)
    current.input['key'] = channel.key
    show_channel(current)
    current.output.update({
        'status': 'Created',
        'code': 201
    })

Response: Create a public channel.


Instruction:
How would you implement a function in Python 3 that
subscribes member to a channel and returns a new list of members.

Response:
def add_members(current):
    """
        Subscribe member(s) to a channel

        .. code-block:: python

            #  request:
                {
                'view':'_zops_add_members',
                'channel_key': key,
                'read_only': boolean, # true if this is a Broadcast channel,
                                      # false if it's a normal chat room
                'members': [key, key],
                }

            #  response:
                {
                'existing': [key,], # existing members
                'newly_added': [key,], # newly added members
                'status': 'Created',
                'code': 201
                }
    """
    newly_added, existing = [], []
    read_only = current.input['read_only']
    for member_key in current.input['members']:
        sb, new = Subscriber(current).objects.get_or_create(user_id=member_key,
                                                            read_only=read_only,
                                                            channel_id=current.input['channel_key'])
        if new:
            newly_added.append(member_key)
        else:
            existing.append(member_key)

    current.output = {
        'existing': existing,
        'newly_added': newly_added,
        'status': 'OK',
        'code': 201
    }


Instruction:
Can you generate a brief explanation for the following Python 3 code
def add_unit_to_channel(current):
    read_only = current.input['read_only']
    newly_added, existing = [], []
    for member_key in UnitModel.get_user_keys(current, current.input['unit_key']):
        sb, new = Subscriber(current).objects.get_or_create(user_id=member_key,
                                                            read_only=read_only,
                                                            channel_id=current.input['channel_key'])
        if new:
            newly_added.append(member_key)
        else:
            existing.append(member_key)

    current.output = {
        'existing': existing,
        'newly_added': newly_added,
        'status': 'OK',
        'code': 201
    }

Response: Subscribes users of a given unit to a given channel


Instruction:
How would you explain what the following Python 3 function does
def search_user(current):
    current.output = {
        'results': [],
        'status': 'OK',
        'code': 201
    }
    qs = UserModel(current).objects.exclude(key=current.user_id).search_on(
        *settings.MESSAGING_USER_SEARCH_FIELDS,
        contains=current.input['query'])
    # FIXME: somehow exclude(key=current.user_id) not working with search_on()

    for user in qs:
        if user.key != current.user_id:
            current.output['results'].append((user.full_name, user.key, user.get_avatar_url()))

Response:
Search users for adding to a public room
            or creating one to one direct messaging


Instruction:
Implement a Python 3 function for
searching on units for subscribing it s users to a channel

Response:
def search_unit(current):
    """
        Search on units for subscribing it's users to a channel

        .. code-block:: python

            #  request:
                {
                'view':'_zops_search_unit',
                'query': string,
                }

            #  response:
                {
                'results': [('name', 'key'), ],
                'status': 'OK',
                'code': 200
                }
    """
    current.output = {
        'results': [],
        'status': 'OK',
        'code': 201
    }
    for user in UnitModel(current).objects.search_on(*settings.MESSAGING_UNIT_SEARCH_FIELDS,
                                                     contains=current.input['query']):
        current.output['results'].append((user.name, user.key))


Instruction:
Implement a function in Python 3 to
create a One - To - One channel between current and selected user.

Response:
def create_direct_channel(current):
    """
    Create a One-To-One channel between current and selected user.

    .. code-block:: python

        #  request:
            {
            'view':'_zops_create_direct_channel',
            'user_key': key,
            }

        #  response:
            {
            'description': string,
            'no_of_members': int,
            'member_list': [
                {'name': string,
                 'is_online': bool,
                 'avatar_url': string,
                }],
            'last_messages': [MSG_DICT]
            'status': 'Created',
            'code': 201,
            'channel_key': key, # of just created channel
            'name': string, # name of subscribed channel
            }
    """
    channel, sub_name = Channel.get_or_create_direct_channel(current.user_id,
                                                             current.input['user_key'])
    current.input['key'] = channel.key
    show_channel(current)
    current.output.update({
        'status': 'Created',
        'code': 201
    })


Instruction:
Implement a Python 3 function for
finding a message in the database.

Response:
def find_message(current):
    """
        Search in messages. If "channel_key" given, search will be limited to that channel,
        otherwise search will be performed on all of user's subscribed channels.

        .. code-block:: python

            #  request:
                {
                'view':'_zops_search_unit,
                'channel_key': key,
                'query': string,
                'page': int,
                }

            #  response:
                {
                'results': [MSG_DICT, ],
                'pagination': {
                    'page': int, # current page
                    'total_pages': int,
                    'total_objects': int,
                    'per_page': int, # object per page
                    },
                'status': 'OK',
                'code': 200
                }
    """
    current.output = {
        'results': [],
        'status': 'OK',
        'code': 201
    }
    query_set = Message(current).objects.search_on(['msg_title', 'body', 'url'],
                                                   contains=current.input['query'])
    if current.input['channel_key']:
        query_set = query_set.filter(channel_id=current.input['channel_key'])
    else:
        subscribed_channels = Subscriber.objects.filter(user_id=current.user_id).values_list(
            "channel_id", flatten=True)
        query_set = query_set.filter(channel_id__in=subscribed_channels)

    query_set, pagination_data = _paginate(current_page=current.input['page'], query_set=query_set)
    current.output['pagination'] = pagination_data
    for msg in query_set:
        current.output['results'].append(msg.serialize(current.user))


Instruction:
Given the following Python 3 function, write the documentation
def delete_channel(current):
    ch_key = current.input['channel_key']

    ch = Channel(current).objects.get(owner_id=current.user_id, key=ch_key)
    ch.delete()
    Subscriber.objects.filter(channel_id=ch_key).delete()
    Message.objects.filter(channel_id=ch_key).delete()
    current.output = {'status': 'Deleted', 'code': 200}

Response:
Delete a channel in the
Taxonomy


Instruction:
Write a Python 3 script for
editing channel name or description.

Response:
def edit_channel(current):
    """
        Update channel name or description

        .. code-block:: python

            #  request:
                {
                'view':'_zops_edit_channel,
                'channel_key': key,
                'name': string,
                'description': string,
                }

            #  response:
                {
                'status': 'OK',
                'code': 200
                }
    """
    ch = Channel(current).objects.get(owner_id=current.user_id,
                                      key=current.input['channel_key'])
    ch.name = current.input['name']
    ch.description = current.input['description']
    ch.save()
    for sbs in ch.subscriber_set.objects.all():
        sbs.name = ch.name
        sbs.save()
    current.output = {'status': 'OK', 'code': 200}


Instruction:
Implement a function in Python 3 to
pin a channel to top of the channel list

Response:
def pin_channel(current):
    """
        Pin a channel to top of channel list

        .. code-block:: python

            #  request:
                {
                'view':'_zops_pin_channel,
                'channel_key': key,
                }

            #  response:
                {
                'status': 'OK',
                'code': 200
                }
    """
    try:
        Subscriber(current).objects.filter(user_id=current.user_id,
                                           channel_id=current.input['channel_key']).update(
            pinned=True)
        current.output = {'status': 'OK', 'code': 200}
    except ObjectDoesNotExist:
        raise HTTPError(404, "")


Instruction:
Implement a function in Python 3 to
delete a message from the resource.

Response:
def delete_message(current):
    """
        Delete a message

        .. code-block:: python

            #  request:
                {
                'view':'_zops_delete_message,
                'message_key': key,
                }

            #  response:
                {
                'key': key,
                'status': 'OK',
                'code': 200
                }
    """
    try:
        Message(current).objects.get(sender_id=current.user_id,
                                     key=current.input['key']).delete()
        current.output = {'status': 'Deleted', 'code': 200, 'key': current.input['key']}
    except ObjectDoesNotExist:
        raise HTTPError(404, "")


Instruction:
Write a Python 3 function that can
edit a message a user own.

Response:
def edit_message(current):
    """
    Edit a message a user own.

    .. code-block:: python

        # request:
        {
            'view':'_zops_edit_message',
            'message': {
                'body': string,     # message text
                'key': key
                }
        }
        # response:
            {
            'status': string,   # 'OK' for success
            'code': int,        # 200 for success
            }

    """
    current.output = {'status': 'OK', 'code': 200}
    in_msg = current.input['message']
    try:
        msg = Message(current).objects.get(sender_id=current.user_id, key=in_msg['key'])
        msg.body = in_msg['body']
        msg.save()
    except ObjectDoesNotExist:
        raise HTTPError(404, "")


Instruction:
Can you generate a brief explanation for the following Python 3 code
def flag_message(current):
    current.output = {'status': 'Created', 'code': 201}
    FlaggedMessage.objects.get_or_create(user_id=current.user_id,
                                         message_id=current.input['key'])

Response:
Flag inappropriate messages

    .. code-block:: python

        # request:
        {
            'view':'_zops_flag_message',
            'message_key': key,
        }
        # response:
            {
            '
            'status': 'Created',
            'code': 201,
            }


Instruction:
Here you have a function in Python 3, explain what it does
def unflag_message(current):
    current.output = {'status': 'OK', 'code': 200}

    FlaggedMessage(current).objects.filter(user_id=current.user_id,
                                           message_id=current.input['key']).delete()

Response: Unflag a message in a node


Instruction:
Explain what the following Python 3 code does
def get_message_actions(current):
    current.output = {'status': 'OK',
                      'code': 200,
                      'actions': Message.objects.get(
                          current.input['key']).get_actions_for(current.user)}

Response: Returns applicable actions for current user for given message key


Instruction:
Can you generate the documentation for the following Python 3 function
def add_to_favorites(current):
    msg = Message.objects.get(current.input['key'])
    current.output = {'status': 'Created', 'code': 201}
    fav, new = Favorite.objects.get_or_create(user_id=current.user_id, message=msg)
    current.output['favorite_key'] = fav.key

Response: Add a new message to the list of favorites.


Instruction:
How would you explain what the following Python 3 function does
def remove_from_favorites(current):
    try:
        current.output = {'status': 'OK', 'code': 200}
        Favorite(current).objects.get(user_id=current.user_id,
                                      key=current.input['key']).delete()
    except ObjectDoesNotExist:
        raise HTTPError(404, "")

Response: Remove a message from the list of user s favorites


Instruction:
Can you tell what is the following Python 3 function doing
def list_favorites(current):
    current.output = {'status': 'OK', 'code': 200, 'favorites': []}
    query_set = Favorite(current).objects.filter(user_id=current.user_id)
    if current.input['channel_key']:
        query_set = query_set.filter(channel_id=current.input['channel_key'])
    current.output['favorites'] = [{
                                       'key': fav.key,
                                       'channel_key': fav.channel.key,
                                       'message_key': fav.message.key,
                                       'message_summary': fav.summary,
                                       'channel_name': fav.channel_name
                                   } for fav in query_set]

Response: List user s favorites. If channel_key is given will return favorites belong to that channel. If user_id is given will return favorites belong to that channel.


Instruction:
Write a Python 3 script to
create a direct messaging channel between two user objects.

Response:
def get_or_create_direct_channel(cls, initiator_key, receiver_key):
        """
        Creates a  direct messaging channel between two user

        Args:
            initiator: User, who want's to make first contact
            receiver: User, other party

        Returns:
            (Channel, receiver_name)
        """
        existing = cls.objects.OR().filter(
            code_name='%s_%s' % (initiator_key, receiver_key)).filter(
            code_name='%s_%s' % (receiver_key, initiator_key))
        receiver_name = UserModel.objects.get(receiver_key).full_name
        if existing:
            channel = existing[0]
        else:
            channel_name = '%s_%s' % (initiator_key, receiver_key)
            channel = cls(is_direct=True, code_name=channel_name, typ=10).blocking_save()
        with BlockSave(Subscriber):
            Subscriber.objects.get_or_create(channel=channel,
                                             user_id=initiator_key,
                                             name=receiver_name)
            Subscriber.objects.get_or_create(channel=channel,
                                             user_id=receiver_key,
                                             name=UserModel.objects.get(initiator_key).full_name)
        return channel, receiver_name


Instruction:
Create a Python 3 function to
create a MQ exchange for this channel.

Response:
def create_exchange(self):
        """
        Creates MQ exchange for this channel
        Needs to be defined only once.
        """
        mq_channel = self._connect_mq()
        mq_channel.exchange_declare(exchange=self.code_name,
                                    exchange_type='fanout',
                                    durable=True)


Instruction:
Can you generate a brief explanation for the following Python 3 code
def delete_exchange(self):
        mq_channel = self._connect_mq()
        mq_channel.exchange_delete(exchange=self.code_name)

Response: Deletes the MQ exchange for this channel.


Instruction:
Here you have a function in Python 3, explain what it does
def get_channel_listing(self):
        return {'name': self.name,
                'key': self.channel.key,
                'type': self.channel.typ,
                'read_only': self.read_only,
                'is_online': self.is_online(),
                'actions': self.get_actions(),
                'unread': self.unread_count()}

Response: Returns a dict of the channel listing


Instruction:
Can you generate the documentation for the following Python 3 function
def create_exchange(self):
        channel = self._connect_mq()
        channel.exchange_declare(exchange=self.user.prv_exchange,
                                 exchange_type='fanout',
                                 durable=True)

Response: Creates a private exchange for the current user


Instruction:
Write a Python 3 script to
bind users private exchange to channel

Response:
def bind_to_channel(self):
        """
        Binds (subscribes) users private exchange to channel exchange
        Automatically called at creation of subscription record.
        """
        if self.channel.code_name != self.user.prv_exchange:
            channel = self._connect_mq()
            channel.exchange_bind(source=self.channel.code_name, destination=self.user.prv_exchange)


Instruction:
Write a Python 3 function that can
serialize the object for given user.

Response:
def serialize(self, user=None):
        """
        Serializes message for given user.

        Note:
            Should be called before first save(). Otherwise "is_update" will get wrong value.

        Args:
            user: User object

        Returns:
            Dict. JSON serialization ready dictionary object
        """
        return {
            'content': self.body,
            'type': self.typ,
            'updated_at': self.updated_at,
            'timestamp': self.updated_at,
            'is_update': not hasattr(self, 'unsaved'),
            'attachments': [attachment.serialize() for attachment in self.attachment_set],
            'title': self.msg_title,
            'url': self.url,
            'sender_name': self.sender.full_name,
            'sender_key': self.sender.key,
            'channel_key': self.channel.key,
            'cmd': 'message',
            'avatar_url': self.sender.avatar,
            'key': self.key,
        }


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

    timestamp = datetime.now().isoformat().replace(':', '_')
    user = getuser()
    return '_'.join(('crawl', user, timestamp))

Response: Provide a reasonable default crawl name using the user name and date


Instruction:
Given the following Python 3 function, write the documentation
def main(argv=None):
    global Verbose, Mock
    if argv is None:
        argv = sys.argv

    if len(argv) < 5: die('Bad args')
    try:
        opts, argv = getopt.getopt(argv[1:], 'hs:p:mv',
          ['help', 'server=', 'port=', 'mock', 'verbose'])
    except getopt.GetoptError as err:
        # print help information and exit:
        print(err) # will print something like "option -a not recognized"
        die()

    serverEndpoint = DefaultServerEndpoint
    # TODO: Fix this
    for opt, val in opts:
        if opt   in ('-h', '--help'):    echo2(USAGE); sys.exit()
        elif opt in ('-s', '--server'):  serverEndpoint = val
        elif opt in ('-p', '--port'):    serverEndpoint = 'http://localhost:%s' % val
        elif opt in ('-m', '--mock'):    Mock = 1
        elif opt in ('-v', '--verbose'): Verbose = 1
        else: die(USAGE)

    cmd = argv[0]
    crawlId = argv[1]
    confId = argv[2]
    urlDir = argv[3]
    args = {}
    if len(argv) > 4: args = eval(argv[4])

    nt = Nutch(crawlId, confId, serverEndpoint, urlDir)
    nt.Jobs().create(cmd, **args)

Response: Run Nutch command using REST API.


Instruction:
Write a Python 3 script for
calling the Nutch Server and return the response.

Response:
def call(self, verb, servicePath, data=None, headers=None, forceText=False, sendJson=True):
        """Call the Nutch Server, do some error checking, and return the response.

        :param verb: One of nutch.RequestVerbs
        :param servicePath: path component of URL to append to endpoint, e.g. '/config'
        :param data: Data to attach to this request
        :param headers: headers to attach to this request, default are JsonAcceptHeader
        :param forceText: don't trust the response headers and just get the text
        :param sendJson: Whether to treat attached data as JSON or not
        """

        default_data = {} if sendJson else ""
        data = data if data else default_data

        headers = headers if headers else JsonAcceptHeader.copy()

        if not sendJson:
            headers.update(TextSendHeader)

        if verb not in RequestVerbs:
            die('Server call verb must be one of %s' % str(RequestVerbs.keys()))
        if Verbose:
            echo2("%s Endpoint:" % verb.upper(), servicePath)
            echo2("%s Request data:" % verb.upper(), data)
            echo2("%s Request headers:" % verb.upper(), headers)
        verbFn = RequestVerbs[verb]

        if sendJson:
            resp = verbFn(self.serverEndpoint + servicePath, json=data, headers=headers)
        else:
            resp = verbFn(self.serverEndpoint + servicePath, data=data, headers=headers)

        if Verbose:
            echo2("Response headers:", resp.headers)
            echo2("Response status:", resp.status_code)
        if resp.status_code != 200:
            if self.raiseErrors:
                error = NutchException("Unexpected server response: %d" % resp.status_code)
                error.status_code = resp.status_code
                raise error
            else:
                warn('Nutch server returned status:', resp.status_code)
        if forceText or 'content-type' not in resp.headers or resp.headers['content-type'] == 'text/plain':
            if Verbose:
                echo2("Response text:", resp.text)
            return resp.text

        content_type = resp.headers['content-type']
        if content_type == 'application/json' and not forceText:
            if Verbose:
                echo2("Response JSON:", resp.json())
            return resp.json()
        else:
            die('Did not understand server response: %s' % resp.headers)


Instruction:
Can you write a function in Python 3 where it
creates a new named ( cid ) configuration from a parameter dictionary.

Response:
def create(self, cid, configData):
        """
        Create a new named (cid) configuration from a parameter dictionary (config_data).
        """
        configArgs = {'configId': cid, 'params': configData, 'force': True}
        cid = self.server.call('post', "/config/create", configArgs, forceText=True, headers=TextAcceptHeader)
        new_config = Config(cid, self.server)
        return new_config


Instruction:
How would you implement a function in Python 3 that
returns a list of all the jobs owned by this Client.

Response:
def list(self, allJobs=False):
        """
        Return list of jobs at this endpoint.

        Call get(allJobs=True) to see all jobs, not just the ones managed by this Client
        """

        jobs = self.server.call('get', '/job')

        return [Job(job['id'], self.server) for job in jobs if allJobs or self._job_owned(job)]


Instruction:
Can you generate a brief explanation for the following Python 3 code
def create(self, command, **args):

        command = command.upper()
        if command not in LegalJobs:
            warn('Nutch command must be one of: %s' % ', '.join(LegalJobs))
        else:
            echo2('Starting %s job with args %s' % (command, str(args)))
        parameters = self.parameters.copy()
        parameters['type'] = command
        parameters['crawlId'] = self.crawlId
        parameters['confId'] = self.confId
        parameters['args'].update(args)

        job_info = self.server.call('post', "/job/create", parameters, JsonAcceptHeader)

        job = Job(job_info['id'], self.server)
        return job

Response: Create a job given a command and args


Instruction:
Can you create a Python 3 function that
injects a new job into a seed list.

Response:
def inject(self, seed=None, urlDir=None, **args):
        """
        :param seed: A Seed object (this or urlDir must be specified)
        :param urlDir: The directory on the server containing the seed list (this or urlDir must be specified)
        :param args: Extra arguments for the job
        :return: a created Job object
        """

        if seed:
            if urlDir and urlDir != seed.seedPath:
                raise NutchException("Can't specify both seed and urlDir")
            urlDir = seed.seedPath
        elif urlDir:
            pass
        else:
            raise NutchException("Must specify seed or urlDir")
        args['url_dir'] = urlDir
        return self.create('INJECT', **args)


Instruction:
Write a Python 3 function that can
create a new named ( sid ) Seed from a list of seed URLs.

Response:
def create(self, sid, seedList):
        """
        Create a new named (sid) Seed from a list of seed URLs

        :param sid: the name to assign to the new seed list
        :param seedList: the list of seeds to use
        :return: the created Seed object
        """

        seedUrl = lambda uid, url: {"id": uid, "url": url}

        if not isinstance(seedList,tuple):
            seedList = (seedList,)

        seedListData = {
            "id": "12345",
            "name": sid,
            "seedUrls": [seedUrl(uid, url) for uid, url in enumerate(seedList)]
        }

        # As per resolution of https://issues.apache.org/jira/browse/NUTCH-2123
        seedPath = self.server.call('post', "/seed/create", seedListData, TextAcceptHeader)
        new_seed = Seed(sid, seedPath, self.server)
        return new_seed


Instruction:
Write a Python 3 function for
creating a new named ( sid ) Seed from a file containing URLs

Response:
def createFromFile(self, sid, filename):
        """
        Create a new named (sid) Seed from a file containing URLs
        It's assumed URLs are whitespace seperated.

        :param sid: the name to assign to the new seed list
        :param filename: the name of the file that contains URLs
        :return: the created Seed object
        """

        urls = []
        with open(filename) as f:
            for line in f:
                for url in line.split():
                    urls.append(url)

        return self.create(sid, tuple(urls))


Instruction:
Implement a function in Python 3 to
start the next job in a given round.

Response:
def _nextJob(self, job, nextRound=True):
        """
        Given a completed job, start the next job in the round, or return None

        :param nextRound: whether to start jobs from the next round if the current round is completed.
        :return: the newly started Job, or None if no job was started
        """

        jobInfo = job.info()
        assert jobInfo['state'] == 'FINISHED'

        roundEnd = False
        if jobInfo['type'] == 'INJECT':
            nextCommand = 'GENERATE'
        elif jobInfo['type'] == 'GENERATE':
            nextCommand = 'FETCH'
        elif jobInfo['type'] == 'FETCH':
            nextCommand = 'PARSE'
        elif jobInfo['type'] == 'PARSE':
            nextCommand = 'UPDATEDB'
        elif jobInfo['type'] == 'UPDATEDB':
            nextCommand = 'INVERTLINKS'
        elif jobInfo['type'] == 'INVERTLINKS':
            nextCommand = 'DEDUP'
        elif jobInfo['type'] == 'DEDUP':
            if self.enable_index:
                nextCommand = 'INDEX'
            else:
                roundEnd = True
        elif jobInfo['type'] == 'INDEX':
            roundEnd = True
        else:
            raise NutchException("Unrecognized job type {}".format(jobInfo['type']))

        if roundEnd:
            if nextRound and self.currentRound < self.totalRounds:
                nextCommand = 'GENERATE'
                self.currentRound += 1
            else:
                return None

        return self.jobClient.create(nextCommand)


Instruction:
Here you have a function in Python 3, explain what it does
def progress(self, nextRound=True):

        currentJob = self.currentJob
        if currentJob is None:
            return currentJob

        jobInfo = currentJob.info()

        if jobInfo['state'] == 'RUNNING':
            return currentJob
        elif jobInfo['state'] == 'FINISHED':
            nextJob = self._nextJob(currentJob, nextRound)
            self.currentJob = nextJob
            return nextJob
        else:
            error = NutchCrawlException("Unexpected job state: {}".format(jobInfo['state']))
            error.current_job = currentJob
            raise NutchCrawlException

Response: Check the status of the current job and activate the next job if it s finished and return the active job.


Instruction:
Write a Python 3 function that can
execute all jobs in the current round and return when they have finished.

Response:
def nextRound(self):
        """
        Execute all jobs in the current round and return when they have finished.

        If a job fails, a NutchCrawlException will be raised, with all completed jobs from this round attached
        to the exception.

        :return: a list of all completed Jobs
        """

        finishedJobs = []
        if self.currentJob is None:
            self.currentJob = self.jobClient.create('GENERATE')

        activeJob = self.progress(nextRound=False)
        while activeJob:
            oldJob = activeJob
            activeJob = self.progress(nextRound=False)  # updates self.currentJob
            if oldJob and oldJob != activeJob:
                finishedJobs.append(oldJob)
            sleep(self.sleepTime)
        self.currentRound += 1
        return finishedJobs


Instruction:
Can you implement a function in Python 3 that
executes all queued rounds and return when they have finished.

Response:
def waitAll(self):
        """
        Execute all queued rounds and return when they have finished.

        If a job fails, a NutchCrawlException will be raised, with all completed jobs attached
        to the exception

        :return: a list of jobs completed for each round, organized by round (list-of-lists)
        """

        finishedRounds = [self.nextRound()]

        while self.currentRound < self.totalRounds:
            finishedRounds.append(self.nextRound())

        return finishedRounds


Instruction:
Can you implement a function in Python 3 that
creates a JobClient for listing and creating jobs.

Response:
def Jobs(self, crawlId=None):
        """
        Create a JobClient for listing and creating jobs.
        The JobClient inherits the confId from the Nutch client.

        :param crawlId: crawlIds to use for this client.  If not provided, will be generated
         by nutch.defaultCrawlId()
        :return: a JobClient
        """
        crawlId = crawlId if crawlId else defaultCrawlId()
        return JobClient(self.server, crawlId, self.confId)


Instruction:
Explain what the following Python 3 code does
def Crawl(self, seed, seedClient=None, jobClient=None, rounds=1, index=True):
        if seedClient is None:
            seedClient = self.Seeds()
        if jobClient is None:
            jobClient = self.Jobs()

        if type(seed) != Seed:
            seed = seedClient.create(jobClient.crawlId + '_seeds', seed)
        return CrawlClient(self.server, seed, jobClient, rounds, index)

Response: Launch a crawl using the given seed and jobClient.


Instruction:
Here you have a function in Python 3, explain what it does
def nginx_access(line):
    '''
    >>> import pprint
    >>> input_line1 = '{ \
                    "remote_addr": "127.0.0.1","remote_user": "-","timestamp": "1515144699.201", \
                    "request": "GET / HTTP/1.1","status": "200","request_time": "0.000", \
                    "body_bytes_sent": "396","http_referer": "-","http_user_agent": "python-requests/2.18.4", \
                    "http_x_forwarded_for": "-","upstream_response_time": "-" \
                        }'
    >>> output_line1 = nginx_access(input_line1)
    >>> pprint.pprint(output_line1)
    {'data': {u'body_bytes_sent': 396.0,
              u'http_referer': u'-',
              u'http_user_agent': u'python-requests/2.18.4',
              u'http_x_forwarded_for': u'-',
              u'remote_addr': u'127.0.0.1',
              u'remote_user': u'-',
              u'request': u'GET / HTTP/1.1',
              u'request_time': 0.0,
              u'status': u'200',
              u'timestamp': '2018-01-05T09:31:39.201000',
              u'upstream_response_time': 0.0},
     'event': 'nginx_event',
     'timestamp': '2018-01-05T09:31:39.201000',
     'type': 'metric'}

    >>> input_line2 = '{ \
                    "remote_addr": "192.158.0.51","remote_user": "-","timestamp": "1515143686.415", \
                    "request": "POST /mpub?topic=heartbeat HTTP/1.1","status": "404","request_time": "0.000", \
                    "body_bytes_sent": "152","http_referer": "-","http_user_agent": "python-requests/2.18.4", \
                    "http_x_forwarded_for": "-","upstream_response_time": "-" \
                       }'
    >>> output_line2 = nginx_access(input_line2)
    >>> pprint.pprint(output_line2)
    {'data': {u'body_bytes_sent': 152.0,
              u'http_referer': u'-',
              u'http_user_agent': u'python-requests/2.18.4',
              u'http_x_forwarded_for': u'-',
              u'remote_addr': u'192.158.0.51',
              u'remote_user': u'-',
              u'request': u'POST /mpub?topic=heartbeat HTTP/1.1',
              u'request_time': 0.0,
              u'status': u'404',
              u'timestamp': '2018-01-05T09:14:46.415000',
              u'upstream_response_time': 0.0},
     'event': 'nginx_event',
     'timestamp': '2018-01-05T09:14:46.415000',
     'type': 'metric'}
    '''
#TODO Handle nginx error logs
    log = json.loads(line)
    timestamp_iso = datetime.datetime.utcfromtimestamp(float(log['timestamp'])).isoformat()
    log.update({'timestamp':timestamp_iso})
    if '-' in log.get('upstream_response_time'):
        log['upstream_response_time'] = 0.0
    log['body_bytes_sent'] = float(log['body_bytes_sent'])
    log['request_time'] = float(log['request_time'])
    log['upstream_response_time'] = float(log['upstream_response_time'])

    return dict(
        timestamp=log.get('timestamp',' '),
        data=log,
        type='metric',
        event='nginx_event',
    )

Response: This function is used to parse the line of a nginx access request.