Instruction:
Can you write a function in Python 3 where it
places a piece at given location.

Response:
def place_piece_at_square(self, piece, location):
        """
        Places piece at given get_location

        :type: piece: Piece
        :type: location: Location
        """
        self.position[location.rank][location.file] = piece
        piece.location = location


Instruction:
Given the following Python 3 function, write the documentation
def move_piece(self, initial, final):
        self.place_piece_at_square(self.piece_at_square(initial), final)
        self.remove_piece_at_square(initial)

Response: Moves a piece from one location to another.


Instruction:
Explain what the following Python 3 code does
def _balance(self, ):
        meta = h.meta
        for n in (skin_reagent.keys() | skin_product.keys()):
            lost = skin_reagent[n]
            cycle_lost = cycle(lost)
            new = skin_product[n]
            cycle_new = cycle(new)
            atom = h._node[n]
            dr = atom.p_radical - atom.radical
            # radical balancing
            if dr > 0:  # radical added or increased.
                for _, m in zip(range(dr), cycle_lost):  # homolysis
                    s_atom = h._node[m]
                    s_atom.p_multiplicity = radical_unmap[s_atom.p_radical + 1]
                    meta.setdefault('rule #14. atom lost. common atom radical added or increased. '
                                    'lost atom radical added', []).append((m, n))
                for m in lost[dr:]:
                    meta.setdefault('rule #15. atom lost. common atom radical added or increased. '
                                    'lost atom radical unchanged', []).append((m, n))
            elif dr < 0:  # radical removed or decreased.
                if n in skin_product:
                    for m in lost:
                        meta.setdefault('rule #20. atom lost. common atom radical removed or decreased. '
                                        'lost atom radical unchanged', []).append((m, n))
                else:
                    for _, m in zip(range(-dr), cycle_lost):  # radical elimination
                        s_atom = h._node[m]
                        s_atom.p_multiplicity = radical_unmap[s_atom.p_radical + 1]
                        meta.setdefault('rule #21. atom lost. common atom radical removed or decreased. '
                                        'lost atom radical added', []).append((m, n))
                    for m in lost[-dr:]:
                        meta.setdefault('rule #20. atom lost. common atom radical removed or decreased. '
                                        'lost atom radical unchanged', []).append((m, n))
            else:
                env = h.environment(n)
                sv = atom.get_valence([(b.reagent, a.reagent) for b, a in env if b.order])
                pv = atom.p_get_valence([(b.product, a.product) for b, a in env if b.p_order])
                sh, ph = h.atom_total_h(n)

                dv = pv - sv
                dh = ph - sh
                dc = atom.p_charge - atom.charge

                if not (dv or dh or dc):  # common atom unchanged. Substitution, Elimination
                    for m in skins:
                        meta.setdefault('rule #1. atom lost. common atom unchanged. '
                                        'substitution, elimination, addition', []).append((m, n))
                elif dv == dh == dc < 0:  # explicit hydrogen removing
                    for m in skins:
                        h._node[m].p_charge = 1
                        meta.setdefault('rule #4. atom lost. common atom deprotonation', []).append((m, n))
                else:
                    for m in skins:
                        meta.setdefault('rule #5. atom lost. common atom changed. '
                                        'convert to reduction or oxidation', []).append((m, n))

                    pth = ph + sum(h.atom_total_h(x)[1] for x in skins)
                    if n in skin_product:
                        sth = sh + sum(h.atom_total_h(x)[0] for x in skin_product[n])
                    else:
                        sth = sh
                    dth = pth - sth

        for n, skins in skin_product.items():
            cycle_skins = cycle(skins)
            atom = h._node[n]
            dr = atom.p_radical - atom.radical
            # radical balancing
            if dr > 0:  # radical added or increased.
                if n in skin_reagent:
                    for m in skins:
                        meta.setdefault('rule #16. atom new. common atom radical added or increased. '
                                        'new atom radical unchanged', []).append((m, n))
                else:
                    for _, m in zip(range(dr), cycle_skins):  # radical addition
                        s_atom = h._node[m]
                        s_atom.multiplicity = radical_unmap[s_atom.radical + 1]
                        meta.setdefault('rule #17. atom new. common atom radical added or increased. '
                                        'new atom radical added', []).append((m, n))
                    for m in skins[dr:]:
                        meta.setdefault('rule #16. atom new. common atom radical added or increased. '
                                        'new atom radical unchanged', []).append((m, n))
            elif dr < 0:  # radical removed or decreased.
                for _, m in zip(range(-dr), cycle_skins):  # recombination
                    s_atom = h._node[m]
                    s_atom.multiplicity = radical_unmap[s_atom.radical + 1]
                    meta.setdefault('rule #18. atom new. common atom radical removed or decreased. '
                                    'new atom radical added', []).append((m, n))
                for m in skins[-dr:]:
                    meta.setdefault('rule #19. atom new. common atom radical removed or decreased. '
                                    'new atom radical unchanged', []).append((m, n))
            else:
                env = h.environment(n)
                sv = atom.get_valence([(b.reagent, a.reagent) for b, a in env if b.order])
                pv = atom.p_get_valence([(b.product, a.product) for b, a in env if b.p_order])
                sh, ph = h.atom_total_h(n)

                dv = pv - sv
                dh = ph - sh
                dc = atom.p_charge - atom.charge

                if not (dv or dh or dc):  # common atom unchanged. Substitution, Addition
                    for m in skins:
                        meta.setdefault('rule #2. atom new. common atom unchanged. '
                                        'substitution, elimination, addition', []).append((m, n))
                elif dv == dh == dc > 0:  # explicit hydrogen addition
                    for m in skins:
                        h._node[m].charge = 1
                        h.meta.setdefault('rule #3. atom new. common atom protonation', []).append((m, n))
                else:
                    for m in skins:
                        meta.setdefault('rule #6. atom new. common atom changed. '
                                        'convert to reduction or oxidation', []).append((m, n))

                    sth = sh + sum(h.atom_total_h(x)[0] for x in skins)
                    if n in skin_reagent:
                        pth = ph + sum(h.atom_total_h(x)[1] for x in skin_reagent[n])
                    else:
                        pth = ph
                    dth = pth - sth

        for n, sp in reverse_ext.items():

            # charge neutralization
            if dc > 0:
                for _ in range(dc):
                    h.meta.setdefault('rule #7. charge neutralization. hydroxide radical added',
                                      []).append(h.add_atom(O(multiplicity=2), O(charge=-1)))
            elif dc < 0:
                for _ in range(-dc):
                    h.meta.setdefault('rule #8. charge neutralization. hydrogen radical added',
                                      []).append(h.add_atom(H(multiplicity=2), H(charge=1)))

            # hydrogen balancing
            if dth > 0:
                red_e = 0
                for m in sp['products']:
                    if h.nodes[m]['element'] == 'H':  # set reduction H if explicit H count increased
                        h.nodes[m]['s_radical'] = 2
                        red_e += 1
                        h.meta.setdefault('rule #11. protonation. new explicit hydrogen radical added',
                                          []).append(m)

                red = []
                for _ in range(dth - red_e):  # add reduction agents
                    m = h.add_atom(H(multiplicity=2), H())
                    red.append(m)
                    h.meta.setdefault('rule #10. protonation. hydrogen radical added', []).append(m)
                red = iter(red)

                dih = sub(*h.atom_implicit_h(n))
                if dih < 0:  # attach reduction H to central atom if implicit H atoms count increased
                    for _ in range(-dih):
                        m = next(red)
                        h.add_bond(m, n, None)
                        h.meta.setdefault('rule #12. protonation. new implicit hydrogen radical added',
                                          []).append(m)

                for m in sp['reagents']:  # attach reduction H if detached group implicit H count increased
                    dih = sub(*h.atom_implicit_h(m))
                    if dih < 0:
                        for _ in range(-dih):
                            o = next(red)
                            h.add_bond(o, m, None)
            elif dth < 0:
                oxo = []
                for _ in range(-dth):
                    m = h.add_atom(O(multiplicity=2), O())
                    oxo.append(m)
                    h.meta.setdefault('rule #9. deprotonation. hydroxide radical added', []).append(m)
                oxo = iter(oxo)

                for m in sp['reagents']:
                    if h.nodes[m]['element'] == 'H':
                        o = next(oxo)
                        h.add_bond(o, m, None)
                        h.meta.setdefault('rule #13. hydrogen accepting by hydroxide radical added',
                                          []).append(m)

        return h

Response: calculate unbalanced charges and radicals for skin atoms


Instruction:
Can you tell what is the following Python 3 function doing
def clone_subgraphs(self, g):
        if not isinstance(g, CGRContainer):
            raise InvalidData('only CGRContainer acceptable')

        r_group = []
        x_group = {}
        r_group_clones = []
        newcomponents = []

        ''' search bond breaks and creations
        '''
        components, lost_bonds, term_atoms = self.__split_graph(g)
        lost_map = {x: y for x, y in lost_bonds}
        ''' extract subgraphs and sort by group type (R or X)
        '''
        x_terminals = set(lost_map.values())
        r_terminals = set(lost_map)

        for i in components:
            x_terminal_atom = x_terminals.intersection(i)
            if x_terminal_atom:
                x_group[x_terminal_atom.pop()] = i
                continue

            r_terminal_atom = r_terminals.intersection(i)
            if r_terminal_atom:
                r_group.append([r_terminal_atom, i])
                continue

            newcomponents.append(i)
        ''' search similar R groups and patch.
        '''
        tmp = g
        for i in newcomponents:
            for k, j in r_group:
                gm = GraphMatcher(j, i, node_match=self.__node_match_products,
                                  edge_match=self.__edge_match_products)
                ''' search for similar R-groups started from bond breaks.
                '''
                mapping = next((x for x in gm.subgraph_isomorphisms_iter() if k.issubset(x) and
                                all(x[y] in term_atoms for y in k)), None)
                if mapping:
                    r_group_clones.append([k, mapping])
                    tmp = compose(tmp, self.__remap_group(j, tmp, mapping)[0])
                    break

        ''' add lose X groups to R groups
        '''
        for i, j in r_group_clones:
            for k in i:
                remappedgroup, mapping = self.__remap_group(x_group[lost_map[k]], tmp, {})
                tmp = CGRcore.union(tmp, remappedgroup)
                tmp.add_edge(j[k], mapping[lost_map[k]], s_bond=1, sp_bond=(1, None))

        if r_group_clones:
            tmp.meta.update(g.meta)
            return tmp

        return tmp.copy()

Response: clone subgraphs from another CGRContainer


Instruction:
Can you generate the documentation for the following Python 3 function
def __get_substitution_paths(g):
        for n, nbrdict in g.adjacency():
            for m, l in combinations(nbrdict, 2):
                nms = nbrdict[m]['sp_bond']
                nls = nbrdict[l]['sp_bond']
                if nms == (1, None) and nls == (None, 1):
                    yield m, n, l
                elif nms == (None, 1) and nls == (1, None):
                    yield l, n, m

Response: get atoms paths from detached atom to attached atom


Instruction:
Explain what the following Python 3 code does
def versions(self):
        if self._versions is None:
            with self.database.cursor_autocommit() as cursor:
                query = """
                SELECT number,
                       date_start,
                       date_done,
                       log,
                       addons
                FROM {}
                """.format(self.table_name)
                cursor.execute(query)
                rows = cursor.fetchall()
                versions = []
                for row in rows:
                    row = list(row)
                    # convert 'addons' to json
                    row[4] = json.loads(row[4]) if row[4] else []
                    versions.append(
                        self.VersionRecord(*row)
                    )
                self._versions = versions
        return self._versions

Response:
Read versions from the table
COOKIE table and store them in self. _versions


Instruction:
Can you write a function in Python 3 where it
returns a dictionary of menu entries for auto - generated models.

Response:
def simple_crud():
        """
        Prepares menu entries for auto-generated model CRUD views.
        This is simple version of :attr:`get_crud_menus()` without
        Category support and permission control.
        Just for development purposes.

        Returns:
            Dict of list of dicts (``{'':[{}],}``). Menu entries.

        """
        results = defaultdict(list)
        for mdl in model_registry.get_base_models():
            results['other'].append({"text": mdl.Meta.verbose_name_plural,
                                     "wf": 'crud',
                                     "model": mdl.__name__,
                                     "kategori": settings.DEFAULT_OBJECT_CATEGORY_NAME})
        return results


Instruction:
How would you implement a function in Python 3 that
generates a list of dicts with keys of object type and values of objects of type CRUD.

Response:
def get_crud_menus(self):
        """
        Generates menu entries according to
        :attr:`zengine.settings.OBJECT_MENU` and permissions
        of current user.

        Returns:
            Dict of list of dicts (``{'':[{}],}``). Menu entries.
        """
        results = defaultdict(list)
        for object_type in settings.OBJECT_MENU:
            for model_data in settings.OBJECT_MENU[object_type]:
                if self.current.has_permission(model_data.get('wf', model_data['name'])):
                    self._add_crud(model_data, object_type, results)
        return results


Instruction:
Can you generate the documentation for the following Python 3 function
def _add_crud(self, model_data, object_type, results):
        model = model_registry.get_model(model_data['name'])
        field_name = model_data.get('field')
        verbose_name = model_data.get('verbose_name', model.Meta.verbose_name_plural)
        category = model_data.get('category', settings.DEFAULT_OBJECT_CATEGORY_NAME)
        wf_dict = {"text": verbose_name,
                   "wf": model_data.get('wf', "crud"),
                   "model": model_data['name'],
                   "kategori": category}
        if field_name:
            wf_dict['param'] = field_name
        results[object_type].append(wf_dict)
        self._add_to_quick_menu(wf_dict['model'], wf_dict)

Response: Adds a new entry for a given object type to the results dict.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def _add_to_quick_menu(self, key, wf):
        if key in settings.QUICK_MENU:
            self.output['quick_menu'].append(wf)

Response: Adds entry to the quick menu according to key.


Instruction:
Given the following Python 3 function, write the documentation
def _get_workflow_menus(self):
        results = defaultdict(list)
        from zengine.lib.cache import WFSpecNames
        for name, title, category in WFSpecNames().get_or_set():
            if self.current.has_permission(name) and category != 'hidden':
                wf_dict = {
                    "text": title,
                    "wf": name,
                    "kategori": category,
                    "param": "id"
                }
                results['other'].append(wf_dict)
                self._add_to_quick_menu(name, wf_dict)
        return results

Response: Creates menu entries for custom workflows.


Instruction:
How would you explain what the following Python 3 function does
def process_request(self, req, resp):
        # req.stream corresponds to the WSGI wsgi.input environ variable,
        # and allows you to read bytes from the request body.
        #
        # See also: PEP 3333
        if req.content_length in (None, 0):
            # Nothing to do
            req.context['data'] = req.params.copy()
            req.context['result'] = {}
            return
        else:
            req.context['result'] = {}

        body = req.stream.read()
        if not body:
            raise falcon.HTTPBadRequest('Empty request body',
                                        'A valid JSON document is required.')

        try:
            json_data = body.decode('utf-8')
            req.context['data'] = json.loads(json_data)
            try:
                log.info("REQUEST DATA: %s" % json_data)
            except:
                log.exception("ERR: REQUEST DATA CANT BE LOGGED ")
        except (ValueError, UnicodeDecodeError):
            raise falcon.HTTPError(falcon.HTTP_753,
                                   'Malformed JSON',
                                   'Could not decode the request body. The '
                                   'JSON was incorrect or not encoded as '
                                   'UTF-8.')

Response: Process the request body and return the object that represents the object.


Instruction:
Can you generate the documentation for the following Python 3 function
def process_response(self, req, resp, resource):
        if 'result' not in req.context:
            return
        req.context['result']['is_login'] = 'user_id' in req.env['session']
        if settings.DEBUG:
            req.context['result']['_debug_queries'] = sys._debug_db_queries
            sys._debug_db_queries = []
        if resp.body is None and req.context['result']:
            resp.body = json.dumps(req.context['result'])

        try:
            log.debug("RESPONSE: %s" % resp.body)
        except:
            log.exception("ERR: RESPONSE CANT BE LOGGED ")

Response: Processes the response from Zengine.


Instruction:
How would you explain what the following Python 3 function does
def connect(self):
        if self.connecting:
            log.info('PikaClient: Already connecting to RabbitMQ')
            return

        log.info('PikaClient: Connecting to RabbitMQ')
        self.connecting = True

        self.connection = TornadoConnection(NON_BLOCKING_MQ_PARAMS,
                                            stop_ioloop_on_close=False,
                                            custom_ioloop=self.io_loop,
                                            on_open_callback=self.on_connected)

Response: Creates connection to RabbitMQ server


Instruction:
Here you have a function in Python 3, explain what it does
def on_connected(self, connection):
        log.info('PikaClient: connected to RabbitMQ')
        self.connected = True
        self.in_channel = self.connection.channel(self.on_channel_open)

Response: Called when RabbitMQ connection is established.


Instruction:
Create a Python 3 function to
input channel creation callback Queue declaration done here Args: channel: input channel

Response:
def on_channel_open(self, channel):
        """
        Input channel creation callback
        Queue declaration done here

        Args:
            channel: input channel
        """
        self.in_channel.exchange_declare(exchange='input_exc', type='topic', durable=True)
        channel.queue_declare(callback=self.on_input_queue_declare, queue=self.INPUT_QUEUE_NAME)


Instruction:
How would you code a function in Python 3 to
input queue declaration callback.

Response:
def on_input_queue_declare(self, queue):
        """
        Input queue declaration callback.
        Input Queue/Exchange binding done here

        Args:
            queue: input queue
        """
        self.in_channel.queue_bind(callback=None,
                                   exchange='input_exc',
                                   queue=self.INPUT_QUEUE_NAME,
                                   routing_key="#")


Instruction:
Given the following Python 3 function, write the documentation
def wsgi_app(self, request):

        try:
            if request.method != 'POST':
                abort(400)

            try:
                # Python 2.7 compatibility
                data = request.data
                if isinstance(data, str):
                    body = json.loads(data)
                else:
                    body = json.loads(data.decode('utf-8'))
            except ValueError:
                abort(400)

            if self.validate:
                valid_cert = util.validate_request_certificate(
                    request.headers, request.data)

                valid_ts = util.validate_request_timestamp(body)

                if not valid_cert or not valid_ts:
                    log.error('failed to validate request')
                    abort(403)

            resp_obj = self.alexa.dispatch_request(body)
            return Response(response=json.dumps(resp_obj, indent=4),
                            status=200,
                            mimetype='application/json')

        except HTTPException as exc:
            log.exception('Failed to handle request')
            return exc

Response: This function is called by the server when a request is received from the server.


Instruction:
Given the following Python 3 function, write the documentation
def close(self, force=False):
        if self.__write:
            self.write = self.__write_adhoc
            self.__write = False

        if not self._is_buffer or force:
            self._file.close()

Response: close the file and all of its children


Instruction:
Here you have a function in Python 3, explain what it does
def _convert_reaction(self, reaction):
        if not (reaction['reactants'] or reaction['products'] or reaction['reagents']):
            raise ValueError('empty reaction')
        maps = {'reactants': [], 'products': [], 'reagents': []}
        for i, tmp in maps.items():
            for molecule in reaction[i]:
                used = set()
                for atom in molecule['atoms']:
                    m = atom['mapping']
                    if m:
                        if m in used:
                            if not self._ignore:
                                raise MappingError('mapping in molecules should be unique')
                            warning(f'non-unique mapping in molecule: {m}')
                        else:
                            used.add(m)
                    tmp.append(m)

        length = count(max(max(maps['products'], default=0), max(maps['reactants'], default=0),
                           max(maps['reagents'], default=0)) + 1)

        ''' map unmapped atoms.
        '''
        for i, tmp in maps.items():
            used = set()
            maps[i] = remap = []
            for m in tmp:
                if not m:
                    remap.append(next(length))
                elif m in used:
                    if not self._ignore:
                        raise MappingError('mapping in reagents or products or reactants should be unique')
                    # force remap non unique atoms in molecules.
                    remap.append(next(length))
                    warning(f'mapping changed: {m} to {remap[-1]}')
                else:
                    remap.append(m)
                    used.add(m)

        if maps['reagents']:
            tmp = (set(maps['reactants']) | set(maps['products'])) & set(maps['reagents'])
            if tmp:
                e = f'reagents has map intersection with reactants or products: {tmp}'
                if not self._ignore:
                    raise MappingError(e)
                warning(e)
                maps['reagents'] = [x if x not in tmp else next(length) for x in maps['reagents']]

        ''' find breaks in map. e.g. 1,2,5,6. 3,4 - skipped
        '''
        if self.__remap:
            lose = sorted(set(range(1, next(length))) - set(maps['reactants']) - set(maps['products']) -
                          set(maps['reagents']), reverse=True)
            if lose:
                for i, tmp in maps.items():
                    if not tmp:
                        continue
                    for j in lose:
                        maps[i] = tmp = [x if x < j else x - 1 for x in tmp]
        ''' end
        '''
        rc = ReactionContainer(meta=reaction['meta'])
        for i, tmp in maps.items():
            shift = 0
            for j in reaction[i]:
                atom_len = len(j['atoms'])
                remapped = {x: y for x, y in enumerate(tmp[shift: atom_len + shift])}
                shift += atom_len
                g = self.__convert_structure(j, remapped)
                rc[i].append(g)
        return rc

Response: Convert reaction to a new reaction.


Instruction:
Can you tell what is the following Python 3 function doing
def aromatize(self):
        rings = [x for x in self.sssr if 4 < len(x) < 7]
        if not rings:
            return 0
        total = 0
        while True:
            c = self._quinonize(rings, 'order')
            if c:
                total += c
            elif total:
                break

            c = self._aromatize(rings, 'order')
            if not c:
                break
            total += c

        if total:
            self.flush_cache()
        return total

Response: convert structure to aromatic form


Instruction:
Write a Python 3 script for
running the crawl job for n rounds

Response:
def crawl_cmd(self, seed_list, n):
        '''
        Runs the crawl job for n rounds
        :param seed_list: lines of seed URLs
        :param n: number of rounds
        :return: number of successful rounds
        '''

        print("Num Rounds "+str(n))

        cc = self.proxy.Crawl(seed=seed_list, rounds=n)
        rounds = cc.waitAll()
        print("Completed %d rounds" % len(rounds))
        return len(rounds)


Instruction:
Implement a Python 3 function for
creating a new config from xml file.

Response:
def load_xml_conf(self, xml_file, id):
        '''
        Creates a new config from xml file.
        :param xml_file: path to xml file. Format : nutch-site.xml or nutch-default.xml
        :param id:
        :return: config object
        '''

        # converting nutch-site.xml to key:value pairs
        import xml.etree.ElementTree as ET
        tree = ET.parse(xml_file)
        params = {}
        for prop in tree.getroot().findall(".//property"):
            params[prop.find('./name').text.strip()] = prop.find('./value').text.strip()
        return self.proxy.Configs().create(id, configData=params)


Instruction:
Write a Python 3 function for
subbing - command AttributeNames Create

Response:
def create_cmd(self, args):
        '''
        'create' sub-command
        :param args: cli arguments
        :return:
        '''
        cmd = args.get('cmd_create')
        if cmd == 'conf':
            conf_file = args['conf_file']
            conf_id = args['id']
            return self.load_xml_conf(conf_file, conf_id)
        else:
            print("Error: Create %s is invalid or not implemented" % cmd)


Instruction:
Implement a Python 3 function for
closing the MRV file and write the close tag of MRV file and close opened file

Response:
def close(self, *args, **kwargs):
        """
        write close tag of MRV file and close opened file

        :param force: force closing of externally opened file or buffer
        """
        if not self.__finalized:
            self._file.write('</cml>')
            self.__finalized = True
        super().close(*args, **kwargs)


Instruction:
Write a Python 3 function that can
write a single molecule or reaction into the file

Response:
def write(self, data):
        """
        write single molecule or reaction into file
        """
        self._file.write('<cml>')
        self.__write(data)
        self.write = self.__write


Instruction:
Can you write a function in Python 3 where it
loads the cache file and return the list of byte offsets from the file

Response:
def _load_cache(self):
        """
        the method is implemented for the purpose of optimization, byte positions will not be re-read from a file
        that has already been used, if the content of the file has changed, and the name has been left the same,
        the old version of byte offsets will be loaded
        :return: list of byte offsets from existing file
        """
        try:
            with open(self.__cache_path, 'rb') as f:
                return load(f)
        except FileNotFoundError:
            return
        except IsADirectoryError as e:
            raise IsADirectoryError(f'Please delete {self.__cache_path} directory') from e
        except (UnpicklingError, EOFError) as e:
            raise UnpicklingError(f'Invalid cache file {self.__cache_path}. Please delete it') from e


Instruction:
Write a Python 3 function for
dumping the cache of the given _shifts into the cache directory.

Response:
def _dump_cache(self, _shifts):
        """
        _shifts dumps in /tmp directory after reboot it will drop
        """
        with open(self.__cache_path, 'wb') as f:
            dump(_shifts, f)


Instruction:
Write a Python 3 function for
getting task types for current user

Response:
def get_task_types(current):
    """
           List task types for current user

           .. code-block:: python

               #  request:
                   {
                   'view': '_zops_get_task_types',
                   }

               #  response:
                   {
                   'task_types': [
                       {'name': string, # wf name
                        'title': string,  # title of workflow
                        },]
                        }
    """
    current.output['task_types'] = [{'name': bpmn_wf.name,
                                     'title': bpmn_wf.title}
                                    for bpmn_wf in BPMNWorkflow.objects.all()
                                    if current.has_permission(bpmn_wf.name)]


Instruction:
Can you tell what is the following Python 3 function doing
def get_task_detail(current):
    task_inv = TaskInvitation.objects.get(current.input['key'])
    obj = task_inv.instance.get_object()
    current.output['task_title'] = task_inv.instance.task.name
    current.output['task_detail'] = """Explain: %s
    State: %s""" % (obj.__unicode__() if obj else '', task_inv.progress)

Response:
Show task details

           .. code-block:: python

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

               #  response:
                   {
                   'task_title': string,
                   'task_detail': string, # markdown formatted text
                    }


Instruction:
Can you implement a function in Python 3 that
returns the list of actions for the current user

Response:
def get_task_actions(current):
    """
           List task types for current user

           .. code-block:: python

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

               #  response:
                   {
                   'key': key,
                   'actions': [{"title":':'Action Title', "wf": "workflow_name"},]
                    }
    """
    task_inv = TaskInvitation.objects.get(current.input['key'])
    actions = [{"title": __(u"Assign Someone Else"), "wf": "assign_same_abstract_role"},
               {"title": __(u"Suspend"), "wf": "suspend_workflow"},
               {"title": __(u"Postpone"), "wf": "postpone_workflow"}]
    if task_inv.instance.current_actor != current.role:
        actions.append({"title": __(u"Assign Yourself"), "wf": "task_assign_yourself"})

    current.output['key'] = task_inv.key
    current.output['actions'] = actions


Instruction:
Can you tell what is the following Python 3 function doing
def get_tasks(current):
    # TODO: Also return invitations for user's other roles
    # TODO: Handle automatic role switching

    STATE_DICT = {
        'active': [20, 30],
        'future': 10,
        'finished': 40,
        'expired': 90
    }
    state = STATE_DICT[current.input['state']]
    if isinstance(state, list):
        queryset = TaskInvitation.objects.filter(progress__in=state)
    else:
        queryset = TaskInvitation.objects.filter(progress=state)

    if 'inverted' in current.input:
        # show other user's tasks
        allowed_workflows = [bpmn_wf.name for bpmn_wf in BPMNWorkflow.objects.all()
                             if current.has_permission(bpmn_wf.name)]
        queryset = queryset.exclude(role_id=current.role_id).filter(wf_name__in=allowed_workflows)
    else:
        # show current user's tasks
        queryset = queryset.filter(role_id=current.role_id)

    if 'query' in current.input:
        queryset = queryset.filter(search_data__contains=current.input['query'].lower())
    if 'wf_type' in current.input:
        queryset = queryset.filter(wf_name=current.input['wf_type'])
    if 'start_date' in current.input:
        queryset = queryset.filter(start_date__gte=datetime.strptime(current.input['start_date'], "%d.%m.%Y"))
    if 'finish_date' in current.input:
        queryset = queryset.filter(finish_date__lte=datetime.strptime(current.input['finish_date'], "%d.%m.%Y"))
    current.output['task_list'] = [
        {
            'token': inv.instance.key,
            'key': inv.key,
            'title': inv.title,
            'wf_type': inv.wf_name,
            'state': inv.progress,
            'start_date': format_date(inv.start_date),
            'finish_date': format_date(inv.finish_date),
            'description': inv.instance.wf.description,
            'status': inv.ownership}
        for inv in queryset
        ]
    task_inv_list = TaskInvitation.objects.filter(role_id=current.role_id)
    current.output['task_count']= {
        'active': task_inv_list.filter(progress__in=STATE_DICT['active']).count(),
        'future' : task_inv_list.filter(progress=STATE_DICT['future']).count(),
        'finished' : task_inv_list.filter(progress=STATE_DICT['finished']).count(),
        'expired' : task_inv_list.filter(progress=STATE_DICT['expired']).count()
    }

Response: Return the list of tasks of the current user.


Instruction:
Given the following Python 3 function, write the documentation
def reduce_memory_usage(df):
    usage_pre = df.memory_usage(deep=True).sum()
    if "runIDs" in df:
        df.loc[:, "runIDs"] = df.loc[:, "runIDs"].astype("category")
    df_int = df.select_dtypes(include=['int'])
    df_float = df.select_dtypes(include=['float'])
    df.loc[:, df_int.columns] = df_int.apply(pd.to_numeric, downcast='unsigned')
    df.loc[:, df_float.columns] = df_float.apply(pd.to_numeric, downcast='float')
    usage_post = df.memory_usage(deep=True).sum()
    logging.info("Reduced DataFrame memory usage from {}Mb to {}Mb".format(
        usage_pre / 1024**2, usage_post / 1024**2))
    if usage_post > 4e9 and "readIDs" in df:
        logging.info("DataFrame of features is too big, dropping read identifiers.")
        return df.drop(["readIDs"], axis=1, errors="ignore")
    else:
        return df

Response: reduce memory usage of the dataframe


Instruction:
Here you have a function in Python 3, explain what it does
def check_existance(f):
    if not opath.isfile(f):
        logging.error("Nanoget: File provided doesn't exist or the path is incorrect: {}".format(f))
        sys.exit("File provided doesn't exist or the path is incorrect: {}".format(f))

Response: Check if the file supplied as input exists.


Instruction:
Here you have a function in Python 3, explain what it does
def list_user_roles(self):
        _form = JsonForm(current=self.current, title=_(u"Switch Role"))
        _form.help_text = "Your current role: %s %s" % (self.current.role.unit.name,
                                                        self.current.role.abstract_role.name)
        switch_roles = self.get_user_switchable_roles()
        _form.role_options = fields.Integer(_(u"Please, choose the role you want to switch:")
                                            , choices=switch_roles, default=switch_roles[0][0],
                                            required=True)
        _form.switch = fields.Button(_(u"Switch"))
        self.form_out(_form)

Response: Lists user roles as selectable except user s current role.


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

        # Get chosen role_key from user form.
        role_key = self.input['form']['role_options']
        # Assign chosen switch role key to user's last_login_role_key field
        self.current.user.last_login_role_key = role_key
        self.current.user.save()
        auth = AuthBackend(self.current)
        # According to user's new role, user's session set again.
        auth.set_user(self.current.user)
        # Dashboard is reloaded according to user's new role.
        self.current.output['cmd'] = 'reload'

Response: Changes user s role from current role to chosen role.


Instruction:
Implement a function in Python 3 to
return a list of tuples user s role list except current role as a tuple

Response:
def get_user_switchable_roles(self):
        """
        Returns user's role list except current role as a tuple
        (role.key, role.name)

        Returns:
            (list): list of tuples, user's role list except current role

        """
        roles = []
        for rs in self.current.user.role_set:
            # rs.role != self.current.role is not True after python version 2.7.12
            if rs.role.key != self.current.role.key:
                roles.append((rs.role.key, '%s %s' % (rs.role.unit.name,
                                                      rs.role.abstract_role.name)))
        return roles


Instruction:
Explain what the following Python 3 code does
def put_object(self, obj):
        # TODO consider putting into a ES class
        self.pr_dbg('put_obj: %s' % self.json_dumps(obj))
        """
        Wrapper for es.index, determines metadata needed to index from obj.
        If you have a raw object json string you can hard code these:
        index is .kibana (as of kibana4);
        id can be A-Za-z0-9\- and must be unique;
        doc_type is either visualization, dashboard, search
            or for settings docs: config, or index-pattern.
        """
        if obj['_index'] is None or obj['_index'] == "":
            raise Exception("Invalid Object, no index")
        if obj['_id'] is None or obj['_id'] == "":
            raise Exception("Invalid Object, no _id")
        if obj['_type'] is None or obj['_type'] == "":
            raise Exception("Invalid Object, no _type")
        if obj['_source'] is None or obj['_source'] == "":
            raise Exception("Invalid Object, no _source")
        self.connect_es()
        self.es.indices.create(index=obj['_index'], ignore=400, timeout="2m")
        try:
            resp = self.es.index(index=obj['_index'],
                                 id=obj['_id'],
                                 doc_type=obj['_type'],
                                 body=obj['_source'], timeout="2m")
        except RequestError as e:
            self.pr_err('RequestError: %s, info: %s' % (e.error, e.info))
            raise
        return resp

Response: Put an object into the index and returns the index entry


Instruction:
Given the following Python 3 function, write the documentation
def del_object(self, obj):
        if obj['_index'] is None or obj['_index'] == "":
            raise Exception("Invalid Object")
        if obj['_id'] is None or obj['_id'] == "":
            raise Exception("Invalid Object")
        if obj['_type'] is None or obj['_type'] == "":
            raise Exception("Invalid Object")
        self.connect_es()
        self.es.delete(index=obj['_index'],
                       id=obj['_id'],
                       doc_type=obj['_type'])

Response: Debug deletes an object of type obj [ _type with id of obj [ _id ]


Instruction:
Create a Python 3 function to
convert obj to json string and write to file

Response:
def write_object_to_file(self, obj, path='.', filename=None):
        """Convert obj (dict) to json string and write to file"""
        output = self.json_dumps(obj) + '\n'
        if filename is None:
            filename = self.safe_filename(obj['_type'], obj['_id'])
        filename = os.path.join(path, filename)
        self.pr_inf("Writing to file: " + filename)
        with open(filename, 'w') as f:
            f.write(output)
        # self.pr_dbg("Contents: " + output)
        return filename


Instruction:
Write a Python 3 script for
writing a list of related objs to file

Response:
def write_pkg_to_file(self, name, objects, path='.', filename=None):
        """Write a list of related objs to file"""
        # Kibana uses an array of docs, do the same
        # as opposed to a dict of docs
        pkg_objs = []
        for _, obj in iteritems(objects):
            pkg_objs.append(obj)
        sorted_pkg = sorted(pkg_objs, key=lambda k: k['_id'])
        output = self.json_dumps(sorted_pkg) + '\n'
        if filename is None:
            filename = self.safe_filename('Pkg', name)
        filename = os.path.join(path, filename)
        self.pr_inf("Writing to file: " + filename)
        with open(filename, 'w') as f:
            f.write(output)
        return filename


Instruction:
Can you generate a brief explanation for the following Python 3 code
def get_objects(self, search_field, search_val):
        query = ("{ size: " + str(self.max_hits) + ", " +
                 "query: { filtered: { filter: { " +
                 search_field + ": { value: \"" + search_val + "\"" +
                 " } } } } } }")
        self.connect_es()
        res = self.es.search(index=self.index, body=query)
        # self.pr_dbg("%d Hits:" % res['hits']['total'])
        objects = {}
        for doc in res['hits']['hits']:
            objects[doc['_id']] = {}
            # To make uploading easier in the future:
            # Record all those bits into the backup.
            # Mimics how ES returns the result.
            # Prevents having to store this in some external, contrived, format
            objects[doc['_id']]['_index'] = self.index  # also in doc['_index']
            objects[doc['_id']]['_type'] = doc['_type']
            objects[doc['_id']]['_id'] = doc['_id']
            objects[doc['_id']]['_source'] = doc['_source']  # the actual result
        return objects

Response: Return all objects of type


Instruction:
Write a Python 3 script for
getting DB and all objs needed to duplicate it

Response:
def get_dashboard_full(self, db_name):
        """Get DB and all objs needed to duplicate it"""
        objects = {}
        dashboards = self.get_objects("type", "dashboard")
        vizs = self.get_objects("type", "visualization")
        searches = self.get_objects("type", "search")
        if db_name not in dashboards:
            return None
        self.pr_inf("Found dashboard: " + db_name)
        objects[db_name] = dashboards[db_name]
        panels = json.loads(dashboards[db_name]['_source']['panelsJSON'])
        for panel in panels:
            if 'id' not in panel:
                continue
            pid = panel['id']
            if pid in searches:
                self.pr_inf("Found search:    " + pid)
                objects[pid] = searches[pid]
            elif pid in vizs:
                self.pr_inf("Found vis:       " + pid)
                objects[pid] = vizs[pid]
                emb = vizs[pid].get('_source', {}).get('savedSearchId', None)
                if emb is not None and emb not in objects:
                    if emb not in searches:
                        self.pr_err('Missing search %s' % emb)
                        return objects
                    objects[emb] = searches[emb]
        return objects


Instruction:
Explain what the following Python 3 code does
def parse_node(self, node):
        spec = super(CamundaProcessParser, self).parse_node(node)
        spec.data = self._parse_input_data(node)
        spec.data['lane_data'] = self._get_lane_properties(node)
        spec.defines = spec.data
        service_class = node.get(full_attr('assignee'))
        if service_class:
            self.parsed_nodes[node.get('id')].service_class = node.get(full_attr('assignee'))
        return spec

Response:
Overrides ProcessParser. parse_node
        Parses and attaches the inputOutput tags that created by Camunda Modeller


Instruction:
How would you explain what the following Python 3 function does
def _get_description(self):
        ns = {'ns': '{%s}' % BPMN_MODEL_NS}
        desc = (
            self.doc_xpath('.//{ns}collaboration/{ns}documentation'.format(**ns)) or
            self.doc_xpath('.//{ns}process/{ns}documentation'.format(**ns)) or
            self.doc_xpath('.//{ns}collaboration/{ns}participant/{ns}documentation'.format(**ns))
        )
        if desc:
            return desc[0].findtext('.')

Response: Tries to get WF description from collabration process or pariticipant


Instruction:
Can you generate a brief explanation for the following Python 3 code
def get_name(self):
        ns = {'ns': '{%s}' % BPMN_MODEL_NS}
        for path in ('.//{ns}process',
                     './/{ns}collaboration',
                     './/{ns}collaboration/{ns}participant/'):
            tag = self.doc_xpath(path.format(**ns))
            if tag:
                name = tag[0].get('name')
                if name:
                    return name
        return self.get_id()

Response: Tries to get WF name from process collobration or pariticipant


Instruction:
How would you code a function in Python 3 to
parse inputOutput part camunda modeller extensions.

Response:
def _parse_input_data(self, node):
        """
        Parses inputOutput part camunda modeller extensions.
        Args:
            node: SpiffWorkflow Node object.

        Returns:
            Data dict.
        """
        data = DotDict()
        try:
            for nod in self._get_input_nodes(node):
                data.update(self._parse_input_node(nod))
        except Exception as e:
            log.exception("Error while processing node: %s" % node)
        return data


Instruction:
How would you implement a function in Python 3 that
parses the given XML node and returns a dictionary of properties for the given lane.

Response:
def _get_lane_properties(self, node):
        """
        Parses the given XML node

        Args:
            node (xml): XML node.

        .. code-block:: xml

             <bpmn2:lane id="Lane_8" name="Lane 8">
                <bpmn2:extensionElements>
                    <camunda:properties>
                        <camunda:property value="foo,bar" name="perms"/>
                    </camunda:properties>
                </bpmn2:extensionElements>
            </bpmn2:lane>

        Returns:
            {'perms': 'foo,bar'}
        """
        lane_name = self.get_lane(node.get('id'))
        lane_data = {'name': lane_name}
        for a in self.xpath(".//bpmn:lane[@name='%s']/*/*/" % lane_name):
            lane_data[a.attrib['name']] = a.attrib['value'].strip()
        return lane_data


Instruction:
Write a Python 3 function for
parsing the XML node to get the key - value pair.

Response:
def _parse_input_node(cls, node):
        """
        :param node: xml node
        :return: dict
        """
        data = {}
        child = node.getchildren()
        if not child and node.get('name'):
            val = node.text
        elif child:  # if tag = "{http://activiti.org/bpmn}script" then data_typ = 'script'
            data_typ = child[0].tag.split('}')[1]
            val = getattr(cls, '_parse_%s' % data_typ)(child[0])
        data[node.get('name')] = val
        return data


Instruction:
How would you code a function in Python 3 to
generate wf packages from workflow diagrams.

Response:
def package_in_memory(cls, workflow_name, workflow_files):
        """
        Generates wf packages from workflow diagrams.

        Args:
            workflow_name: Name of wf
            workflow_files:  Diagram  file.

        Returns:
            Workflow package (file like) object
        """
        s = StringIO()
        p = cls(s, workflow_name, meta_data=[])
        p.add_bpmn_files_by_glob(workflow_files)
        p.create_package()
        return s.getvalue()


Instruction:
Write a Python 3 function that can
compose a reaction container from another reaction container

Response:
def compose(self, data):
        """
        condense reaction container to CGR. see init for details about cgr_type

        :param data: ReactionContainer
        :return: CGRContainer
        """
        g = self.__separate(data) if self.__cgr_type in (1, 2, 3, 4, 5, 6) else self.__condense(data)
        g.meta.update(data.meta)
        return g


Instruction:
Can you create a Python 3 function that
adds atom to the internal dictionary

Response:
def add_atom(self, atom, _map=None):
        """
        new atom addition
        """
        if _map is None:
            _map = max(self, default=0) + 1
        elif _map in self._node:
            raise KeyError('atom with same number exists')

        attr_dict = self.node_attr_dict_factory()
        if isinstance(atom, str):
            attr_dict.element = atom
        elif isinstance(atom, int):
            attr_dict.element = elements_list[atom - 1]
        else:
            attr_dict.update(atom)

        self._adj[_map] = self.adjlist_inner_dict_factory()
        self._node[_map] = attr_dict
        self.flush_cache()
        return _map


Instruction:
Can you implement a function in Python 3 that
returns a tuple of pairs of ( bond atom ) connected to atom

Response:
def environment(self, atom):
        """
        pairs of (bond, atom) connected to atom

        :param atom: number
        :return: list
        """
        return tuple((bond, self._node[n]) for n, bond in self._adj[atom].items())


Instruction:
Write a Python 3 script to
create a substructure containing atoms from nbunch list

Response:
def substructure(self, atoms, meta=False, as_view=True):
        """
        create substructure containing atoms from nbunch list

        :param atoms: list of atoms numbers of substructure
        :param meta: if True metadata will be copied to substructure
        :param as_view: If True, the returned graph-view provides a read-only view
            of the original structure scaffold without actually copying any data.
        """
        s = self.subgraph(atoms)
        if as_view:
            s.add_atom = s.add_bond = s.delete_atom = s.delete_bond = frozen  # more informative exception
            return s
        s = s.copy()
        if not meta:
            s.graph.clear()
        return s


Instruction:
Create a Python 3 function for
creating a substructure of the graph with the given atoms and their neighbors

Response:
def augmented_substructure(self, atoms, dante=False, deep=1, meta=False, as_view=True):
        """
        create substructure containing atoms and their neighbors

        :param atoms: list of core atoms in graph
        :param dante: if True return list of graphs containing atoms, atoms + first circle, atoms + 1st + 2nd,
            etc up to deep or while new nodes available
        :param deep: number of bonds between atoms and neighbors
        :param meta: copy metadata to each substructure
        :param as_view: If True, the returned graph-view provides a read-only view
            of the original graph without actually copying any data
        """
        nodes = [set(atoms)]
        for i in range(deep):
            n = {y for x in nodes[-1] for y in self._adj[x]} | nodes[-1]
            if n in nodes:
                break
            nodes.append(n)
        if dante:
            return [self.substructure(a, meta, as_view) for a in nodes]
        else:
            return self.substructure(nodes[-1], meta, as_view)


Instruction:
How would you implement a function in Python 3 that
splits disconnected structure to connected substructures

Response:
def split(self, meta=False):
        """
        split disconnected structure to connected substructures

        :param meta: copy metadata to each substructure
        :return: list of substructures
        """
        return [self.substructure(c, meta, False) for c in connected_components(self)]


Instruction:
Can you generate the documentation for the following Python 3 function
def bonds(self):
        seen = set()
        for n, m_bond in self._adj.items():
            seen.add(n)
            for m, bond in m_bond.items():
                if m not in seen:
                    yield n, m, bond

Response: iterate other all bonds


Instruction:
How would you explain what the following Python 3 function does
def _get_subclass(name):
        return next(x for x in BaseContainer.__subclasses__() if x.__name__ == name)

Response: get the subclass of the given name


Instruction:
How would you code a function in Python 3 to
create a urllib3. PoolManager object that has SSL verification enabled and uses the certifi certificates.

Response:
def _default_make_pool(http, proxy_info):
    """Creates a urllib3.PoolManager object that has SSL verification enabled
    and uses the certifi certificates."""

    if not http.ca_certs:
        http.ca_certs = _certifi_where_for_ssl_version()

    ssl_disabled = http.disable_ssl_certificate_validation

    cert_reqs = 'CERT_REQUIRED' if http.ca_certs and not ssl_disabled else None

    if isinstance(proxy_info, collections.Callable):
        proxy_info = proxy_info()
    if proxy_info:
        if proxy_info.proxy_user and proxy_info.proxy_pass:
            proxy_url = 'http://{}:{}@{}:{}/'.format(
                proxy_info.proxy_user, proxy_info.proxy_pass,
                proxy_info.proxy_host, proxy_info.proxy_port,
            )
            proxy_headers = urllib3.util.request.make_headers(
                proxy_basic_auth='{}:{}'.format(
                    proxy_info.proxy_user, proxy_info.proxy_pass,
                )
            )
        else:
            proxy_url = 'http://{}:{}/'.format(
                proxy_info.proxy_host, proxy_info.proxy_port,
            )
            proxy_headers = {}

        return urllib3.ProxyManager(
            proxy_url=proxy_url,
            proxy_headers=proxy_headers,
            ca_certs=http.ca_certs,
            cert_reqs=cert_reqs,
        )
    return urllib3.PoolManager(
        ca_certs=http.ca_certs,
        cert_reqs=cert_reqs,
    )


Instruction:
Can you generate a brief explanation for the following Python 3 code
def patch(make_pool=_default_make_pool):
    setattr(httplib2, '_HttpOriginal', httplib2.Http)
    httplib2.Http = Http
    Http._make_pool = make_pool

Response: Monkey - patches httplib2. Http to be httplib2shim. Http.


Instruction:
Can you write a function in Python 3 where it
checks if a given address is an IPv6 address.

Response:
def _is_ipv6(addr):
    """Checks if a given address is an IPv6 address."""
    try:
        socket.inet_pton(socket.AF_INET6, addr)
        return True
    except socket.error:
        return False


Instruction:
Write a Python 3 function for
getting the right location for certifi certifications for the current SSLVersion.

Response:
def _certifi_where_for_ssl_version():
    """Gets the right location for certifi certifications for the current SSL
    version.

    Older versions of SSL don't support the stronger set of root certificates.
    """
    if not ssl:
        return

    if ssl.OPENSSL_VERSION_INFO < (1, 0, 2):
        warnings.warn(
            'You are using an outdated version of OpenSSL that '
            'can\'t use stronger root certificates.')
        return certifi.old_where()

    return certifi.where()


Instruction:
Create a Python 3 function to
map a urllib3 response to a httplib2 Response.

Response:
def _map_response(response, decode=False):
    """Maps a urllib3 response to a httplib/httplib2 Response."""
    # This causes weird deepcopy errors, so it's commented out for now.
    # item._urllib3_response = response
    item = httplib2.Response(response.getheaders())
    item.status = response.status
    item['status'] = str(item.status)
    item.reason = response.reason
    item.version = response.version

    # httplib2 expects the content-encoding header to be stripped and the
    # content length to be the length of the uncompressed content.
    # This does not occur for 'HEAD' requests.
    if decode and item.get('content-encoding') in ['gzip', 'deflate']:
        item['content-length'] = str(len(response.data))
        item['-content-encoding'] = item.pop('content-encoding')

    return item


Instruction:
Write a Python 3 script to
map an exception from urlib3 to httplib2.

Response:
def _map_exception(e):
    """Maps an exception from urlib3 to httplib2."""
    if isinstance(e, urllib3.exceptions.MaxRetryError):
        if not e.reason:
            return e
        e = e.reason
    message = e.args[0] if e.args else ''
    if isinstance(e, urllib3.exceptions.ResponseError):
        if 'too many redirects' in message:
            return httplib2.RedirectLimit(message)
    if isinstance(e, urllib3.exceptions.NewConnectionError):
        if ('Name or service not known' in message or
                'nodename nor servname provided, or not known' in message):
            return httplib2.ServerNotFoundError(
                'Unable to find hostname.')
        if 'Connection refused' in message:
            return socket.error((errno.ECONNREFUSED, 'Connection refused'))
    if isinstance(e, urllib3.exceptions.DecodeError):
        return httplib2.FailedToDecompressContent(
            'Content purported as compressed but not uncompressable.',
            httplib2.Response({'status': 500}), '')
    if isinstance(e, urllib3.exceptions.TimeoutError):
        return socket.timeout('timed out')
    if isinstance(e, urllib3.exceptions.SSLError):
        return ssl.SSLError(*e.args)

    return e


Instruction:
Can you write a function in Python 3 where it
starts workers with no_subprocess and watch_paths

Response:
def run_workers(no_subprocess, watch_paths=None, is_background=False):
    """
    subprocess handler
    """
    import atexit, os, subprocess, signal
    if watch_paths:
        from watchdog.observers import Observer
        # from watchdog.observers.fsevents import FSEventsObserver as Observer
        # from watchdog.observers.polling import PollingObserver as Observer
        from watchdog.events import FileSystemEventHandler

    def on_modified(event):
        if not is_background:
            print("Restarting worker due to change in %s" % event.src_path)
        log.info("modified %s" % event.src_path)
        try:
            kill_children()
            run_children()
        except:
            log.exception("Error while restarting worker")

    handler = FileSystemEventHandler()
    handler.on_modified = on_modified

    # global child_pids
    child_pids = []
    log.info("starting %s workers" % no_subprocess)

    def run_children():
        global child_pids
        child_pids = []
        for i in range(int(no_subprocess)):
            proc = subprocess.Popen([sys.executable, __file__],
                                    stdout=subprocess.PIPE,
                                    stderr=subprocess.PIPE)
            child_pids.append(proc.pid)
            log.info("Started worker with pid %s" % proc.pid)

    def kill_children():
        """
        kill subprocess on exit of manager (this) process
        """
        log.info("Stopping worker(s)")
        for pid in child_pids:
            if pid is not None:
                os.kill(pid, signal.SIGTERM)

    run_children()
    atexit.register(kill_children)
    signal.signal(signal.SIGTERM, kill_children)
    if watch_paths:
        observer = Observer()
        for path in watch_paths:
            if not is_background:
                print("Watching for changes under %s" % path)
            observer.schedule(handler, path=path, recursive=True)
        observer.start()
    while 1:
        try:
            sleep(1)
        except KeyboardInterrupt:
            log.info("Keyboard interrupt, exiting")
            if watch_paths:
                observer.stop()
                observer.join()
            sys.exit(0)


Instruction:
Can you generate the documentation for the following Python 3 function
def exit(self, signal=None, frame=None):
        self.input_channel.close()
        self.client_queue.close()
        self.connection.close()
        log.info("Worker exiting")
        sys.exit(0)

Response: Properly close the AMQP connections


Instruction:
Create a Python 3 function for
making amqp connection and create channels and queue binding

Response:
def connect(self):
        """
        make amqp connection and create channels and queue binding
        """
        self.connection = pika.BlockingConnection(BLOCKING_MQ_PARAMS)
        self.client_queue = ClientQueue()
        self.input_channel = self.connection.channel()

        self.input_channel.exchange_declare(exchange=self.INPUT_EXCHANGE,
                                            type='topic',
                                            durable=True)
        self.input_channel.queue_declare(queue=self.INPUT_QUEUE_NAME)
        self.input_channel.queue_bind(exchange=self.INPUT_EXCHANGE, queue=self.INPUT_QUEUE_NAME)
        log.info("Bind to queue named '%s' queue with exchange '%s'" % (self.INPUT_QUEUE_NAME,
                                                                        self.INPUT_EXCHANGE))


Instruction:
Write a Python 3 function for
clearing outs all messages from INPUT_QUEUE_NAME

Response:
def clear_queue(self):
        """
        clear outs all messages from INPUT_QUEUE_NAME
        """
        def remove_message(ch, method, properties, body):
            print("Removed message: %s" % body)
        self.input_channel.basic_consume(remove_message, queue=self.INPUT_QUEUE_NAME, no_ack=True)
        try:
            self.input_channel.start_consuming()
        except (KeyboardInterrupt, SystemExit):
            log.info(" Exiting")
            self.exit()


Instruction:
How would you explain what the following Python 3 function does
def run(self):
        self.input_channel.basic_consume(self.handle_message,
                                         queue=self.INPUT_QUEUE_NAME,
                                         no_ack=True
                                         )
        try:
            self.input_channel.start_consuming()
        except (KeyboardInterrupt, SystemExit):
            log.info(" Exiting")
            self.exit()

Response: This method is run when the main thread is started.


Instruction:
Make a summary of the following Python 3 code
def get_progress(start, finish):
    now = datetime.now()
    dif_time_start = start - now
    dif_time_finish = finish - now

    if dif_time_start.days < 0 and dif_time_finish.days < 0:
        return PROGRESS_STATES[3][0]
    elif dif_time_start.days < 0 and dif_time_finish.days >= 1:
        return PROGRESS_STATES[2][0]
    elif dif_time_start.days >= 1 and dif_time_finish.days >= 1:
        return PROGRESS_STATES[0][0]
    else:
        return PROGRESS_STATES[2][0]

Response: Returns the state of the current time range.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def sync_wf_cache(current):
    wf_cache = WFCache(current)
    wf_state = wf_cache.get()  # unicode serialized json to dict, all values are unicode
    if 'role_id' in wf_state:
        # role_id inserted by engine, so it's a sign that we get it from cache not db
        try:
            wfi = WFInstance.objects.get(key=current.input['token'])
        except ObjectDoesNotExist:
            # wf's that not started from a task invitation
            wfi = WFInstance(key=current.input['token'])
            wfi.wf = BPMNWorkflow.objects.get(name=wf_state['name'])
        if not wfi.current_actor.exist:
            # we just started the wf
            try:
                inv = TaskInvitation.objects.get(instance=wfi, role_id=wf_state['role_id'])
                inv.delete_other_invitations()
                inv.progress = 20
                inv.save()
            except ObjectDoesNotExist:
                current.log.exception("Invitation not found: %s" % wf_state)
            except MultipleObjectsReturned:
                current.log.exception("Multiple invitations found: %s" % wf_state)
        wfi.step = wf_state['step']
        wfi.name = wf_state['name']
        wfi.pool = wf_state['pool']
        wfi.current_actor_id = str(wf_state['role_id'])  # keys must be str not unicode
        wfi.data = wf_state['data']
        if wf_state['finished']:
            wfi.finished = True
            wfi.finish_date = wf_state['finish_date']
            wf_cache.delete()
        wfi.save()

    else:
        # if cache already cleared, we have nothing to sync
        pass

Response: Synchronizes the state of a specific wf with the cache.


Instruction:
Can you create a Python 3 function that
returns a diagram XML instance and whether or not it s already exists.

Response:
def get_or_create_by_content(cls, name, content):
        """
        if xml content updated, create a new entry for given wf name
        Args:
            name: name of wf
            content: xml content

        Returns (DiagramXML(), bool): A tuple with two members.
        (DiagramXML instance and True if it's new or False it's already exists
        """
        new = False
        diagrams = cls.objects.filter(name=name)
        if diagrams:
            diagram = diagrams[0]
            if diagram.body != content:
                new = True
        else:
            new = True
        if new:
            diagram = cls(name=name, body=content).save()
        return diagram, new


Instruction:
Create a Python 3 function for
trying to get WF description from collabration process or pariticipant

Response:
def get_description(self):
        """
        Tries to get WF description from 'collabration' or 'process' or 'pariticipant'

        Returns str: WF description

        """
        paths = ['bpmn:collaboration/bpmn:participant/bpmn:documentation',
                 'bpmn:collaboration/bpmn:documentation',
                 'bpmn:process/bpmn:documentation']
        for path in paths:
            elm = self.root.find(path, NS)
            if elm is not None and elm.text:
                return elm.text


Instruction:
How would you code a function in Python 3 to
try to get WF name from process collobration or pariticipant

Response:
def get_name(self):
        """
        Tries to get WF name from 'process' or 'collobration' or 'pariticipant'

        Returns:
            str. WF name.
        """
        paths = ['bpmn:process',
                 'bpmn:collaboration/bpmn:participant/',
                 'bpmn:collaboration',
                 ]
        for path in paths:
            tag = self.root.find(path, NS)
            if tag is not None and len(tag):
                name = tag.get('name')
                if name:
                    return name


Instruction:
Here you have a function in Python 3, explain what it does
def set_xml(self, diagram, force=False):
        no_of_running = WFInstance.objects.filter(wf=self, finished=False, started=True).count()
        if no_of_running and not force:
            raise RunningInstancesExist(
                "Can't update WF diagram! Running %s WF instances exists for %s" % (
                    no_of_running, self.name
                ))
        else:
            self.xml = diagram
            parser = BPMNParser(diagram.body)
            self.description = parser.get_description()
            self.title = parser.get_name() or self.name.replace('_', ' ').title()
            extensions = dict(parser.get_wf_extensions())
            self.programmable = extensions.get('programmable', False)
            self.task_type = extensions.get('task_type', None)
            self.menu_category = extensions.get('menu_category', settings.DEFAULT_WF_CATEGORY_NAME)
            self.save()

Response: Updates the xml link of the current object with the given diagram.


Instruction:
Write a Python 3 script for
creating wf instances. Args: roles (list): role list Returns: (list): wf instances

Response:
def create_wf_instances(self, roles=None):
        """
        Creates wf instances.
        Args:
            roles (list): role list

        Returns:
            (list): wf instances
        """

        # if roles specified then create an instance for each role
        # else create only one instance

        if roles:
            wf_instances = [
                WFInstance(
                    wf=self.wf,
                    current_actor=role,
                    task=self,
                    name=self.wf.name
                ) for role in roles
                ]
        else:
            wf_instances = [
                WFInstance(
                    wf=self.wf,
                    task=self,
                    name=self.wf.name
                )
            ]

        # if task type is not related with objects save instances immediately.
        if self.task_type in ["C", "D"]:
            return [wfi.save() for wfi in wf_instances]

        # if task type is related with its objects, save populate instances per object
        else:
            wf_obj_instances = []
            for wfi in wf_instances:
                role = wfi.current_actor if self.task_type == "A" else None
                keys = self.get_object_keys(role)
                wf_obj_instances.extend(
                    [WFInstance(
                        wf=self.wf,
                        current_actor=role,
                        task=self,
                        name=self.wf.name,
                        wf_object=key,
                        wf_object_type=self.object_type
                    ).save() for key in keys]
                )

            return wf_obj_instances


Instruction:
How would you code a function in Python 3 to
create a list of WFInstance per object and TaskInvitation per role.

Response:
def create_tasks(self):
        """
        will create a WFInstance per object
        and per TaskInvitation for each role and WFInstance
        """
        roles = self.get_roles()

        if self.task_type in ["A", "D"]:
            instances = self.create_wf_instances(roles=roles)
            self.create_task_invitation(instances)

        elif self.task_type in ["C", "B"]:
            instances = self.create_wf_instances()
            self.create_task_invitation(instances, roles)


Instruction:
Implement a function in Python 3 to
return objects keys according to self. object_query_code alid

Response:
def get_object_query_dict(self):
        """returns objects keys according to self.object_query_code
        which can be json encoded queryset filter dict or key=value set
         in the following format: ```"key=val, key2 = val2 , key3= value with spaces"```

        Returns:
             (dict): Queryset filtering dicqt
        """
        if isinstance(self.object_query_code, dict):
            # _DATE_ _DATETIME_
            return self.object_query_code
        else:
            # comma separated, key=value pairs. wrapping spaces will be ignored
            # eg: "key=val, key2 = val2 , key3= value with spaces"
            return dict(pair.split('=') for pair in self.object_query_code.split(','))


Instruction:
Can you create a Python 3 function that
returns object keys according to task definition which can be explicitly selected one object

Response:
def get_object_keys(self, wfi_role=None):
        """returns object keys according to task definition
        which can be explicitly selected one object (self.object_key) or
        result of a queryset filter.

        Returns:
            list of object keys
        """
        if self.object_key:
            return [self.object_key]
        if self.object_query_code:
            model = model_registry.get_model(self.object_type)
            return [m.key for m in
                    self.get_model_objects(model, wfi_role, **self.get_object_query_dict())]


Instruction:
Explain what the following Python 3 code does
def get_model_objects(model, wfi_role=None, **kwargs):
        query_dict = {}
        for k, v in kwargs.items():
            if isinstance(v, list):
                query_dict[k] = [str(x) for x in v]
            else:
                parse = str(v).split('.')
                if parse[0] == 'role' and wfi_role:
                    query_dict[k] = wfi_role
                    for i in range(1, len(parse)):
                        query_dict[k] = query_dict[k].__getattribute__(parse[i])
                else:
                    query_dict[k] = parse[0]

        return model.objects.all(**query_dict)

Response: Fetches all model objects by filtering with kwargs


Instruction:
Can you create a Python 3 function that
returns a list of all the roles that are defined in the current task definition.

Response:
def get_roles(self):
        """
        Returns:
            Role instances according to task definition.
        """
        if self.role.exist:
            # return explicitly selected role
            return [self.role]
        else:
            roles = []
            if self.role_query_code:
                #  use given "role_query_code"
                roles = RoleModel.objects.filter(**self.role_query_code)
            elif self.unit.exist:
                # get roles from selected unit or sub-units of it
                if self.recursive_units:
                    # this returns a list, we're converting it to a Role generator!
                    roles = (RoleModel.objects.get(k) for k in
                             UnitModel.get_role_keys(self.unit.key))
                else:
                    roles = RoleModel.objects.filter(unit=self.unit)
            elif self.get_roles_from:
                # get roles from selected predefined "get_roles_from" method
                return ROLE_GETTER_METHODS[self.get_roles_from](RoleModel)

        if self.abstract_role.exist and roles:
            # apply abstract_role filtering on roles we got
            if isinstance(roles, (list, types.GeneratorType)):
                roles = [a for a in roles if a.abstract_role.key == self.abstract_role.key]
            else:
                roles = roles.filter(abstract_role=self.abstract_role)
        else:
            roles = RoleModel.objects.filter(abstract_role=self.abstract_role)

        return roles


Instruction:
How would you explain what the following Python 3 function does
def delete_other_invitations(self):
        # TODO: Signal logged-in users to remove the task from their task list
        self.objects.filter(instance=self.instance).exclude(key=self.key).delete()

Response: When one person use an invitation we should delete other invitations


Instruction:
Write a Python 3 script to
save the wf state to the cache

Response:
def save(self, wf_state):
        """
        write wf state to DB through MQ >> Worker >> _zops_sync_wf_cache

        Args:
            wf_state dict: wf state
        """
        self.wf_state = wf_state
        self.wf_state['role_id'] = self.current.role_id
        self.set(self.wf_state)
        if self.wf_state['name'] not in settings.EPHEMERAL_WORKFLOWS:
            self.publish(job='_zops_sync_wf_cache',
                         token=self.db_key)


Instruction:
Given the following Python 3 function, write the documentation
def send_to_default_exchange(self, sess_id, message=None):
        msg = json.dumps(message, cls=ZEngineJSONEncoder)
        log.debug("Sending following message to %s queue through default exchange:\n%s" % (
            sess_id, msg))
        self.get_channel().publish(exchange='', routing_key=sess_id, body=msg)

Response: Send a message through RabbitMQ s default exchange.


Instruction:
Write a Python 3 function that can
send messages through logged in users private exchange.

Response:
def send_to_prv_exchange(self, user_id, message=None):
        """
        Send messages through logged in users private exchange.

        Args:
            user_id string: User key
            message dict: Message object

        """
        exchange = 'prv_%s' % user_id.lower()
        msg = json.dumps(message, cls=ZEngineJSONEncoder)
        log.debug("Sending following users \"%s\" exchange:\n%s " % (exchange, msg))
        self.get_channel().publish(exchange=exchange, routing_key='', body=msg)


Instruction:
Write a Python 3 function that can
compose two graphs to a CGRContainer

Response:
def compose(self, other):
        """
        compose 2 graphs to CGR

        :param other: Molecule or CGR Container
        :return: CGRContainer
        """
        if not isinstance(other, Compose):
            raise TypeError('CGRContainer or MoleculeContainer [sub]class expected')

        cgr = self._get_subclass('CGRContainer')
        common = self._node.keys() & other

        if not common:
            if not (isinstance(self, cgr) or isinstance(other, cgr)):
                return cgr() | self | other
            return self | other

        unique_reactant = self._node.keys() - common
        unique_product = other._node.keys() - common

        h = cgr()
        atoms = h._node
        bonds = []
        common_adj = {n: {} for n in common}
        common_bonds = []

        r_atoms = {}
        r_skin = defaultdict(list)
        if isinstance(self, cgr):
            for n in unique_reactant:
                h.add_atom(self._node[n], n)
                for m, bond in self._adj[n].items():
                    if m not in atoms:
                        if m in common:  # bond to common atoms is broken bond
                            r_bond = bond._reactant
                            if r_bond is None:  # skip None>None
                                continue
                            r_skin[n].append(m)
                            bond = DynBond.__new__(DynBond)
                            bond.__init_copy__(r_bond, None)
                        bonds.append((n, m, bond))
            for n in common:
                r_atoms[n] = self._node[n]._reactant
                for m, bond in self._adj[n].items():
                    if m not in r_atoms and m in common:
                        tmp = [bond._reactant, None]
                        common_adj[n][m] = common_adj[m][n] = tmp
                        common_bonds.append((n, m, tmp))
        else:
            for n in unique_reactant:
                atom = DynAtom.__new__(DynAtom)  # add unique atom into CGR
                atom.__init_copy__(self._node[n], self._node[n])
                h.add_atom(atom, n)
                for m, r_bond in self._adj[n].items():  # unique atom neighbors
                    if m not in atoms:  # bond not analyzed yet
                        bond = DynBond.__new__(DynBond)
                        if m in common:  # bond to common atoms
                            r_skin[n].append(m)
                            bond.__init_copy__(r_bond, None)
                        else:  # bond static
                            bond.__init_copy__(r_bond, r_bond)
                        bonds.append((n, m, bond))
            for n in common:
                r_atoms[n] = self._node[n]
                for m, bond in self._adj[n].items():
                    if m not in r_atoms and m in common:  # analyze only common atoms bonds
                        tmp = [bond, None]  # reactant state only
                        common_adj[n][m] = common_adj[m][n] = tmp
                        common_bonds.append((n, m, tmp))

        p_atoms = {}
        p_skin = defaultdict(list)
        if isinstance(other, cgr):
            for n in unique_product:
                h.add_atom(other._node[n], n)
                for m, bond in other._adj[n].items():
                    if m not in atoms:
                        if m in common:  # bond to common atoms is new bond
                            p_bond = bond._product
                            if p_bond is None:  # skip None>None
                                continue
                            p_skin[n].append(m)
                            bond = DynBond.__new__(DynBond)
                            bond.__init_copy__(None, p_bond)
                        bonds.append((n, m, bond))
            for n in common:
                p_atoms[n] = other._node[n]._product
                n_bonds = common_adj[n]
                for m, bond in other._adj[n].items():
                    if m in n_bonds:
                        n_bonds[m][1] = bond._product
                    elif m not in p_atoms and m in common:  # new bond of reaction
                        p_bond = bond._product
                        if p_bond is None:  # skip None>None
                            continue
                        bond = DynBond.__new__(DynBond)
                        bond.__init_copy__(None, p_bond)
                        bonds.append((n, m, bond))
        else:
            for n in unique_product:
                atom = DynAtom.__new__(DynAtom)
                atom.__init_copy__(other._node[n], other._node[n])
                h.add_atom(atom, n)
                for m, p_bond in other._adj[n].items():
                    if m not in atoms:
                        bond = DynBond.__new__(DynBond)
                        if m in common:
                            p_skin[n].append(m)
                            bond.__init_copy__(None, p_bond)
                        else:
                            bond.__init_copy__(p_bond, p_bond)
                        bonds.append((n, m, bond))
            for n in common:
                p_atoms[n] = other._node[n]
                n_bonds = common_adj[n]
                for m, p_bond in other._adj[n].items():
                    if m in n_bonds:  # set product state of changed bond
                        n_bonds[m][1] = p_bond
                    elif m not in p_atoms and m in common:  # new bond of reaction
                        bond = DynBond.__new__(DynBond)
                        bond.__init_copy__(None, p_bond)
                        bonds.append((n, m, bond))

        for n, r_atom in r_atoms.items():  # prepare common DynAtom's
            p_atom = p_atoms[n]
            if r_atom.element != p_atom.element or r_atom.isotope != p_atom.isotope:
                raise ValueError('atom-to-atom mapping invalid')
            atom = DynAtom.__new__(DynAtom)
            atom.__init_copy__(r_atom, p_atom)
            h.add_atom(atom, n)

        for n, m, (r_bond, p_bond) in common_bonds:
            if r_bond is p_bond is None:  # skip None>None
                continue
            bond = DynBond.__new__(DynBond)
            bond.__init_copy__(r_bond, p_bond)
            h.add_bond(n, m, bond)

        for n, m, bond in bonds:
            h.add_bond(n, m, bond)

        return h


Instruction:
Write a Python 3 script to
decompose the CGR to pair of Molecules which represents reactants and products state of reaction .

Response:
def decompose(self):
        """
        decompose CGR to pair of Molecules, which represents reactants and products state of reaction

        :return: tuple of two molecules
        """
        mc = self._get_subclass('MoleculeContainer')
        reactants = mc()
        products = mc()

        for n, atom in self.atoms():
            reactants.add_atom(atom._reactant, n)
            products.add_atom(atom._product, n)

        for n, m, bond in self.bonds():
            if bond._reactant is not None:
                reactants.add_bond(n, m, bond._reactant)
            if bond._product is not None:
                products.add_bond(n, m, bond._product)
        return reactants, products


Instruction:
How would you explain what the following Python 3 function does
def cycle_data(self, verbose=False, result_cycle=None, result_size=None, result_edges=None,changelog=True):

        cycle_names = [s['name'] for s in self.settings['cycle']]
        accepted_steps = set(s['name'] for s in self.settings['cycle'] if s['type'] == StatusTypes.accepted)
        completed_steps = set(s['name'] for s in self.settings['cycle'] if s['type'] == StatusTypes.complete)

        series = {
            'key': {'data': [], 'dtype': str},
            'url': {'data': [], 'dtype': str},
            'issue_type': {'data': [], 'dtype': str},
            'summary': {'data': [], 'dtype': str},
            'status': {'data': [], 'dtype': str},
            'resolution': {'data': [], 'dtype': str},
            'cycle_time': {'data': [], 'dtype': 'timedelta64[ns]'},
            'completed_timestamp': {'data': [], 'dtype': 'datetime64[ns]'},
            'created_timestamp': {'data': [], 'dtype': 'datetime64[ns]'}
        }

        if sys.platform.startswith('win'):
            buffer = open("cycledata.tmp", "w+",1)
            # Opens a file for writing only in binary format. Overwrites the file if the file exists.
            # buffering value is 1
            # Windows users seem to have a problem with spooled file
        else:
            buffer = tempfile.SpooledTemporaryFile(max_size=50000, mode='w+t')

        #issuelinks = open("issuelinks.csv", "w+", 1)
        #df_edges = pd.DataFrame()
        #df_edges = pd.DataFrame(columns=['Source', 'OutwardLink', 'Target', 'Inwardlink','LinkType'])
        #df_edges.to_csv(issuelinks, columns=['Source', 'OutwardLink', 'Target', 'Inwardlink','LinkType'], header=True, index=None, sep='\t',encoding='utf-8')

        df_size_history = pd.DataFrame( columns=['key','fromDate','toDate','size'])
        df_size_history.to_csv(buffer, columns=['key', 'fromDate', 'toDate', 'size'], header=True, index=None, sep='\t',encoding='utf-8')

        for cycle_name in cycle_names:
            series[cycle_name] = {'data': [], 'dtype': 'datetime64[ns]'}

        for name in self.fields.keys():
            series[name] = {'data': [], 'dtype': 'object'}

        if self.settings['query_attribute']:
            series[self.settings['query_attribute']] = {'data': [], 'dtype': str}

        for criteria in self.settings['queries']:
            for issue in self.find_issues(criteria, order='updatedDate DESC', verbose=verbose, changelog=changelog):

                # Deal with the differences in strings between Python 2 & 3
                if (sys.version_info > (3, 0)):
                    # Python 3 code in this block
                    item = {
                        'key': issue.key,
                        'url': "%s/browse/%s" % (self.jira._options['server'], issue.key,),
                        'issue_type': issue.fields.issuetype.name,
                        'summary': issue.fields.summary,  # .encode('utf-8'),
                        'status': issue.fields.status.name,
                        'resolution': issue.fields.resolution.name if issue.fields.resolution else None,
                        'cycle_time': None,
                        'completed_timestamp': None,
                        'created_timestamp':  issue.fields.created[:19]
                    }
                else:
                    # Python 2 code in this block
                    item = {
                        'key': issue.key,
                        'url': "%s/browse/%s" % (self.jira._options['server'], issue.key,),
                        'issue_type': issue.fields.issuetype.name,
                        'summary': issue.fields.summary.encode('utf-8'),
                        'status': issue.fields.status.name,
                        'resolution': issue.fields.resolution.name if issue.fields.resolution else None,
                        'cycle_time': None,
                        'completed_timestamp': None,
                        'created_timestamp': issue.fields.created[:19]
                    }

                for name, field_name in self.fields.items():
                    item[name] = self.resolve_field_value(issue, name, field_name)

                if self.settings['query_attribute']:
                    item[self.settings['query_attribute']] = criteria.get('value', None)

                for cycle_name in cycle_names:
                    item[cycle_name] = None

                # Get the relationships for this issue
                edges = []  # Source, Target, Inward Link, Outward Link, Type
                issuelinks = issue.fields.issuelinks

                # It is seems that having an Epic Parent does not record an Epic Link, just the name "Epic Name"
                # Creating Epic relationship requires more work. Also each Jira instance will have different customfields for Epic data
                # Remove this code.
                #issueEpic = issue.fields.customfield_10008 if issue.fields.customfield_10008 else None  # Epic Link
                #if issueEpic is not None:
                #    data = {'Source':issueEpic, 'Target':issue.key, 'InwardLink':'Belongs to Epic', 'OutwardLink':'Issue in Epic', 'LinkType':'EpicIssue'}
                #    edges.append(data)

                for link in issuelinks:
                    inwardissue = None
                    outwardissue = None
                    try:
                        inwardissue = link.inwardIssue.key
                    except:
                        outwardissue = link.outwardIssue.key

                    if inwardissue is not None:
                        data = {'LinkID':link.id,'Source':inwardissue, 'Target':issue.key, 'InwardLink':link.type.inward, 'OutwardLink': link.type.outward, 'LinkType':link.type.name}
                    else:
                        data = {'LinkID':link.id,'Source':issue.key, 'Target': outwardissue, 'InwardLink':link.type.inward, 'OutwardLink':link.type.outward, 'LinkType':link.type.name}
                    edges.append(data)

                if len(edges)>0:
                    try:
                        df_edges
                    except NameError:
                        #print('Not found')
                        df_edges = pd.DataFrame(edges)
                    else:
                        df_links = pd.DataFrame(edges)
                        df_edges=df_edges.append(df_links)  # = pd.DataFrame(edges)
                # Got all the relationships for this issue

                rows = []
                try:
                    for snapshot in self.iter_size_changes(issue):
                        data= {'key':snapshot.key,'fromDate':snapshot.date,'size':snapshot.size}
                        rows.append(data)

                    df = pd.DataFrame(rows)
                    # Create the toDate column
                    df_toDate=df['fromDate'].shift(-1)
                    df_toDate.loc[len(df_toDate)-1] = datetime.datetime.now(pytz.utc)
                    df['toDate'] = df_toDate
                except:
                    df = pd.DataFrame(columns = ['key', 'fromDate', 'toDate', 'size'])

                # Round Down datetimes to full dates
                df['fromDate'] = df['fromDate'].apply(lambda dt: datetime.datetime(dt.year, dt.month, dt.day))
                df['toDate'] = df['toDate'].apply(lambda dt: datetime.datetime(dt.year, dt.month, dt.day))
                # If we only have one row of size changes and current issue has a size then it must have been created with a size value at creation.
                # This size will not be recorded in the size_change record.
                # Hence update the single row we have with the current issue size.
                # Get Story Points size changes history
                #If condition is met update the size cell
                if getattr(item, 'StoryPoints', None) is not None and (df.shape[0]==1):
                #if (item['StoryPoints'] is not None ) and (len(df)==1):
                    df.loc[df.index[0], 'size'] = item['StoryPoints']

                # Append to csv file
                df.to_csv(buffer, columns=['key', 'fromDate', 'toDate', 'size'], header=None,
                           mode='a', sep='\t', date_format='%Y-%m-%d',encoding='utf-8')
                #print(rows)

                # If the first column in item lifecycle was scipted put the created data in it.
                if item[cycle_names[0]] is None:
                    item[cycle_names[0]] = dateutil.parser.parse(item['created_timestamp']) #item['created_timestamp']
                    # Figure out why the first Column does not have created date
                    #print(dateutil.parser.parse(item['created_timestamp']))

                # Record date of status changes
                for snapshot in self.iter_changes(issue, True):
                    snapshot_cycle_step = self.settings['cycle_lookup'].get(snapshot.status.lower(), None)
                    if snapshot_cycle_step is None:
                        if verbose:
                            print(issue.key, "transitioned to unknown JIRA status", snapshot.status)
                        continue

                    snapshot_cycle_step_name = snapshot_cycle_step['name']

                    # Keep the first time we entered a step
                    if item[snapshot_cycle_step_name] is None:
                        item[snapshot_cycle_step_name] = snapshot.date

                    # Wipe any subsequent dates, in case this was a move backwards
                    found_cycle_name = False
                    for cycle_name in cycle_names:
                        if not found_cycle_name and cycle_name == snapshot_cycle_step_name:
                            found_cycle_name = True
                            continue
                        elif found_cycle_name and item[cycle_name] is not None:
                            if verbose:
                                print(issue.key, "moved backwards to", snapshot_cycle_step_name, "wiping date for subsequent step", cycle_name)
                            item[cycle_name] = None

                # Wipe timestamps if items have moved backwards; calculate cycle time

                previous_timestamp = None
                accepted_timestamp = None
                completed_timestamp = None

                for cycle_name in cycle_names:
                    if item[cycle_name] is not None:
                        previous_timestamp = item[cycle_name]

                        if accepted_timestamp is None and previous_timestamp is not None and cycle_name in accepted_steps:
                            accepted_timestamp = previous_timestamp
                        if completed_timestamp is None and previous_timestamp is not None and cycle_name in completed_steps:
                            completed_timestamp = previous_timestamp

                if accepted_timestamp is not None and completed_timestamp is not None:
                    item['cycle_time'] = completed_timestamp - accepted_timestamp
                    item['completed_timestamp'] = completed_timestamp

                for k, v in item.items():
                    series[k]['data'].append(v)

        data = {}
        for k, v in series.items():
            data[k] = pd.Series(v['data'], dtype=v['dtype'])

        result_cycle = pd.DataFrame(data,
                    columns=['key', 'url', 'issue_type', 'summary', 'status', 'resolution'] +
                    sorted(self.fields.keys()) +
                    ([self.settings['query_attribute']] if self.settings['query_attribute'] else []) +
                    ['cycle_time', 'completed_timestamp'] +
                    cycle_names
        )

        result_size = pd.DataFrame()
        buffer.seek(0)
        result_size = result_size.from_csv(buffer, sep='\t')
        buffer.close()

        try:
            df_edges
        except NameError:
            # print('Not found')
            df_edges = pd.DataFrame()
        try:
            df_edges = df_edges[['Source', 'OutwardLink', 'Target', 'InwardLink','LinkType','LinkID']] # Specify dataframe sort order
            #df_edges.to_csv("myedges.csv", sep='\t', index=False,encoding='utf-8')
        except KeyError:
            print('Info: No issue edges found.')

        result_edges=df_edges

        # There maybe no result_size data is we might not have any change history
        try:
            result_size.set_index('key')
        except KeyError:
            result_size = pd.DataFrame(index= ['key'],columns = ['fromDate', 'toDate', 'size'])

        result_size['toDate'] = pd.to_datetime(result_size['toDate'], format=('%Y-%m-%d'))
        result_size['fromDate'] = pd.to_datetime(result_size['fromDate'], format=('%Y-%m-%d'))

        return result_cycle, result_size, result_edges

Response: Get data from JIRA for cycle times and story points size change.