Instruction:
Write a Python 3 script to
add a callback to the list of listeners that will be called when the value modification of a key is received.

Response:
def listen(self, you):
        """
        Request a callback for value modification.

        Parameters
        ----------
        you : object
            An instance having ``__call__`` attribute.
        """
        self._listeners.append(you)
        self.raw.talk_to(you)


Instruction:
Can you generate the documentation for the following Python 3 function
def _get_term_by_id(self, id):
        '''Simple utility function to load a term.
        '''
        url = (self.url + '/%s.json') % id
        r = self.session.get(url)
        return r.json()

Response: Simple utility function to load a term.


Instruction:
Create a Python 3 function to
return all concepts or collections that form the top-level of a display hierarchy. As opposed to the :meth:`get_top_concepts`, this method can possibly return both concepts and collections. :rtype: Returns a list of concepts and collections. For each an id is present and a label. The label is determined by looking at the `**kwargs` parameter, the default language of the provider and falls back to `en` if nothing is present.

Response:
def get_top_display(self, **kwargs):
        '''
        Returns all concepts or collections that form the top-level of a display
        hierarchy.

        As opposed to the :meth:`get_top_concepts`, this method can possibly
        return both concepts and collections.

        :rtype: Returns a list of concepts and collections. For each an
            id is present and a label. The label is determined by looking at
            the `**kwargs` parameter, the default language of the provider
            and falls back to `en` if nothing is present.
        '''
        language = self._get_language(**kwargs)
        url = self.url + '/lijst.json'
        args = {'type[]': ['HR']}
        r = self.session.get(url, params=args)
        result = r.json()
        items = result
        top = self.get_by_id(items[0]['id'])
        res = []
        def expand_coll(res, coll):
            for nid in coll.members:
                c = self.get_by_id(nid)
                res.append({
                    'id': c.id,
                    'label': c.label(language)
                })
            return res
        return expand_coll(res, top)


Instruction:
Can you write a function in Python 3 where it
returns a list of concepts or collections that should be displayed under this concept or collection.

Response:
def get_children_display(self, id, **kwargs):
        '''
        Return a list of concepts or collections that should be displayed
        under this concept or collection.

        :param id: A concept or collection id.
        :rtype: A list of concepts and collections. For each an
            id is present and a label. The label is determined by looking at
            the `**kwargs` parameter, the default language of the provider
            and falls back to `en` if nothing is present. If the id does not
            exist, return `False`.
        '''
        language = self._get_language(**kwargs)
        item = self.get_by_id(id)
        res = []
        if isinstance(item, Collection):
            for mid in item.members:
                m = self.get_by_id(mid)
                res.append({
                    'id': m.id,
                    'label': m.label(language)
                })
        else:
            for cid in item.narrower:
                c = self.get_by_id(cid)
                res.append({
                    'id': c.id,
                    'label': c.label(language)
                })
        return res


Instruction:
How would you implement a function in Python 3 that
translates a list of identifiers from one gene to another.

Response:
def translate_genes(id_list=None, from_id=None, to_id=None, organism=None):
    """
    Pass a list of identifiers (id_list), the name of the database ('Entrez',
    'Symbol', 'Standard name', 'Systematic name' or a loaded crossreference
    database) that you wish to translate from, and the name of the database
    that you wish to translate to.
    """

    ids = set(id_list)
    # Initialize set of identifiers not found by this translate_genes method.
    not_found = set()
    from_ids = None  # Get the map of from_ids to the gene pks

    if organism is not None:
        gene_objects_manager = Gene.objects.filter(
            organism__scientific_name=organism)
    else:
        gene_objects_manager = Gene.objects

    if (from_id == 'Entrez'):
        int_list = []
        for x in ids:
            try:
                int_list.append(int(x))
            except(ValueError):
                not_found.add(x)
        ids = set(int_list)
        from_ids = gene_objects_manager.filter(entrezid__in=ids).values_list(
            'entrezid', 'id')
    elif (from_id == 'Systematic name'):
        from_ids = gene_objects_manager.filter(
            systematic_name__in=ids).values_list('systematic_name', 'id')
    elif (from_id == 'Standard name'):
        from_ids = gene_objects_manager.filter(
            standard_name__in=ids).values_list('standard_name', 'id')
    elif (from_id == 'Symbol'):
        # If standard_name exists, symbol will be standard_name; otherwise
        # symbol will be systematic_name.
        from_ids = gene_objects_manager.annotate(
            symbol=Coalesce('standard_name', 'systematic_name')).filter(
                symbol__in=ids).values_list('symbol', 'id')
    else:  # a crossreference db?
        xrdb = CrossRefDB.objects.get(name=from_id)
        from_ids = CrossRef.objects.filter(crossrefdb=xrdb).values_list(
            'xrid', 'gene__id')

    # Dictionary that maps from type ID passed by user to gene__id.
    from_id_map = {}
    gene_ids = []
    for item in from_ids:
        from_id_map[item[0]] = item[1]
        gene_ids.append(item[1])

    # Now let's figure out what we need to go to:
    to_ids = None
    if (to_id == 'Entrez'):
        to_ids = Gene.objects.filter(id__in=gene_ids).values_list(
            'id', 'entrezid')
    elif (to_id == 'Systematic name'):
        to_ids = Gene.objects.filter(id__in=gene_ids).values_list(
            'id', 'systematic_name')
    elif (to_id == 'Standard name'):
        to_ids = Gene.objects.filter(id__in=gene_ids).values_list(
            'id', 'standard_name')
    elif (to_id == 'Symbol'):
        # If standard_name exists, symbol will be standard_name; otherwise
        # symbol will be systematic_name.
        to_ids = Gene.objects.annotate(
            symbol=Coalesce('standard_name', 'systematic_name')).filter(
                id__in=gene_ids).values_list('id', 'symbol')

    else:  # A crossreference db?
        xrdb = CrossRefDB.objects.get(name=to_id)
        to_ids = CrossRef.objects.filter(crossrefdb=xrdb).values_list(
            'gene__id', 'xrid')

    to_id_map = {}
    for item in to_ids:
        if not item[0] in to_id_map:
            to_id_map[item[0]] = [item[1], ]
        else:
            to_id_map[item[0]].append(item[1])

    from_to = {}
    for item in ids:
        try:
            gene_id = from_id_map[item]
        except KeyError:
            not_found.add(item)
            continue
        to_id = to_id_map[gene_id]
        from_to[item] = to_id
    from_to['not_found'] = list(not_found)

    return from_to


Instruction:
Implement a function in Python 3 to
draw a cross centered in the target area.

Response:
def Cross(width=3, color=0):
    """Draws a cross centered in the target area

    :param width: width of the lines of the cross in pixels
    :type width: int
    :param color: color of the lines of the cross
    :type color: pygame.Color
    """
    return Overlay(Line("h", width, color), Line("v", width, color))


Instruction:
Write a Python 3 function that can
top level function to create a tuple of nodes.

Response:
def compose(target, root=None):
    """Top level function to create a surface.

    :param target: the pygame.Surface to blit on. Or a (width, height) tuple
        in which case a new surface will be created

    :type target: -
    """
    if type(root) == Surface:
        raise ValueError("A Surface may not be used as root, please add "
                        +"it as a single child i.e. compose(...)(Surface(...))")
    @_inner_func_anot
    def inner_compose(*children):
        if root:
            root_context = root(*children)
        else:
            assert len(children) == 1
            root_context = children[0]

        if type(target) == pygame.Surface:
            surface = target
            size = target.get_size()
        else:
            size = target
            surface = pygame.Surface(size)

        root_context._draw(surface, pygame.Rect(0, 0, *size))
        return surface
    return inner_compose


Instruction:
Can you implement a function in Python 3 that
returns a font object for the current object.

Response:
def Font(name=None, source="sys", italic=False, bold=False, size=20):
    """Unifies loading of fonts.

    :param name: name of system-font or filepath, if None is passed the default
        system-font is loaded

    :type name: str
    :param source: "sys" for system font, or "file" to load a file
    :type source: str
    """
    assert source in ["sys", "file"]
    if not name:
        return pygame.font.SysFont(pygame.font.get_default_font(),
        size, bold=bold, italic=italic)
    if source == "sys":
        return pygame.font.SysFont(name,
        size, bold=bold, italic=italic)
    else:
        f = pygame.font.Font(name, size)
        f.set_italic(italic)
        f.set_bold(bold)
        return f


Instruction:
Explain what the following Python 3 code does
def Text(text, font, color=pygame.Color(0, 0, 0), antialias=False, align="center"):
    assert align in ["center", "left", "right"]
    margin_l, margin_r = 1, 1
    if align == "left": margin_l = 0
    elif align == "right": margin_r = 0
    margin = Margin(margin_l, margin_r)
    color_key = pygame.Color(0, 0, 1) if pygame.Color(0, 0, 1) != color else 0x000002

    text_surfaces = _lmap(lambda text: _text(text, font=font,
                                   color=color, antialias=antialias),
                                   map(methodcaller("strip"), text.split("\n")))
    w = max(surf.get_rect().w for surf in text_surfaces)
    h = sum(surf.get_rect().h for surf in text_surfaces)
    surf = compose((w, h), Fill(color_key))(LinLayout("v")(
        *_lmap(lambda s: Surface(margin)(s), text_surfaces)))
    surf.set_colorkey(color_key)
    return surf.convert_alpha()

Response: Renders a text. Supports multiline text.


Instruction:
Write a Python 3 function for
creating a padding by the remaining space after scaling the content.

Response:
def from_scale(scale_w, scale_h=None):
        """Creates a padding by the remaining space after scaling the content.

        E.g. Padding.from_scale(0.5) would produce Padding(0.25, 0.25, 0.25, 0.25) and
        Padding.from_scale(0.5, 1) would produce Padding(0.25, 0.25, 0, 0)
        because the content would not be scaled (since scale_h=1) and therefore
        there would be no vertical padding.

        If scale_h is not specified scale_h=scale_w is used as default

        :param scale_w: horizontal scaling factors
        :type scale_w: float
        :param scale_h: vertical scaling factor
        :type scale_h: float
        """
        if not scale_h: scale_h = scale_w
        w_padding = [(1 - scale_w) * 0.5] * 2
        h_padding = [(1 - scale_h) * 0.5] * 2
        return Padding(*w_padding, *h_padding)


Instruction:
Make a summary of the following Python 3 code
def radicals(self, levels=None):
        url = WANIKANI_BASE.format(self.api_key, 'radicals')
        if levels:
            url += '/{0}'.format(levels)
        data = self.get(url)

        for item in data['requested_information']:
            yield Radical(item)

Response: Gets the list of radicals for a given set of levels.


Instruction:
How would you code a function in Python 3 to
return an iterator over all Kanji items.

Response:
def kanji(self, levels=None):
        """
        :param levels: An optional argument of declaring a single or
            comma-delimited list of levels is available, as seen in the example
            as 1. An example of a comma-delimited list of levels is 1,2,5,9.
        :type levels: str or None

        http://www.wanikani.com/api/v1.2#kanji-list
        """
        url = WANIKANI_BASE.format(self.api_key, 'kanji')
        if levels:
            url += '/{0}'.format(levels)
        data = self.get(url)

        for item in data['requested_information']:
            yield Kanji(item)


Instruction:
Can you write a function in Python 3 where it
returns a generator that yields all available or obsolete vocabulary items.

Response:
def vocabulary(self, levels=None):
        """
        :param levels: An optional argument of declaring a single or
            comma-delimited list of levels is available, as seen in the example
            as 1. An example of a comma-delimited list of levels is 1,2,5,9.
        :type levels: str or None

        http://www.wanikani.com/api/v1.2#vocabulary-list
        """

        url = WANIKANI_BASE.format(self.api_key, 'vocabulary')
        if levels:
            url += '/{0}'.format(levels)
        data = self.get(url)

        if 'general' in data['requested_information']:
            for item in data['requested_information']['general']:
                yield Vocabulary(item)
        else:
            for item in data['requested_information']:
                yield Vocabulary(item)


Instruction:
Explain what the following Python 3 code does
def ishex(obj):
    return isinstance(obj, str) and (len(obj) == 1) and (obj in string.hexdigits)

Response: Test if the argument is a string representing a valid hexadecimal digit.


Instruction:
Write a Python 3 script for
testing if the argument is a number.

Response:
def isnumber(obj):
    """
    Test if the argument is a number (complex, float or integer).

    :param obj: Object
    :type  obj: any

    :rtype: boolean
    """
    return (
        (obj is not None)
        and (not isinstance(obj, bool))
        and isinstance(obj, (int, float, complex))
    )


Instruction:
Explain what the following Python 3 code does
def isreal(obj):
    return (
        (obj is not None)
        and (not isinstance(obj, bool))
        and isinstance(obj, (int, float))
    )

Response: Test if the argument is a real number.


Instruction:
Given the following Python 3 function, write the documentation
def create_api_context(self, cls):
        return self.api_context_schema().load({
            "name": cls.name,
            "cls": cls,
            "inst": [],
            "conf": self.conf.get_api_service(cls.name),
            "calls": self.conf.get_api_calls(),
            "shared": {},  # Used per-API to monitor state
            "log_level": self.conf.get_log_level(),
            "callback": self.receive
            })

Response: Create and return an API context object


Instruction:
Create a Python 3 function to
pass an API result down the pipeline

Response:
def receive(self, data, api_context):
        """Pass an API result down the pipeline"""
        self.log.debug(f"Putting data on the pipeline: {data}")
        result = {
            "api_contexts": self.api_contexts,
            "api_context": api_context,
            "strategy": dict(),  # Shared strategy data
            "result": data,
            "log_level": api_context["log_level"],
        }
        self.strat.execute(self.strategy_context_schema().load(result).data)


Instruction:
Here you have a function in Python 3, explain what it does
def course(self):
        course = self.parent
        while course.parent:
            course = course.parent
        return course

Response: Returns the Course this node belongs to


Instruction:
Make a summary of the following Python 3 code
def path(self):
        if self.parent is None:
            return self.title
        return join(self.parent.path, self.title)

Response: Returns the path of this node on Studip.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def title(self):
        tmp = c.namemap_lookup(self.id) if c.namemap_lookup(self.id) is not None else self._title
        return secure_filename(tmp)

Response: get the title of this node


Instruction:
Explain what the following Python 3 code does
def deep_documents(self):
        tree = []
        for entry in self.contents:
            if isinstance(entry, Document):
                tree.append(entry)
            else:
                tree += entry.deep_documents
        return tree

Response: returns a list of all documents find in subtrees of this node


Instruction:
Given the following Python 3 function, write the documentation
def title(self):
        name = c.namemap_lookup(self.id)
        if name is None:
            name = self._title + " " + client.get_semester_title(self)
            c.namemap_set(self.id, name)
        return secure_filename(name)

Response: Returns the title of the course.


Instruction:
Write a Python 3 function that can
run a get request against a url. Returns the response which can optionally be streamed

Response:
def _get(self, route, stream=False):
        """
        run a get request against an url. Returns the response which can optionally be streamed
        """
        log.debug("Running GET request against %s" % route)
        return r.get(self._url(route), auth=c.auth, stream=stream)


Instruction:
Can you generate the documentation for the following Python 3 function
def get_contents(self, folder: Folder):
        log.debug("Listing Contents of %s/%s" % (folder.course.id, folder.id))
        if isinstance(folder, Course):
            response = json.loads(self._get('/api/documents/%s/folder' % folder.course.id).text)
        else:
            response = json.loads(self._get('/api/documents/%s/folder/%s' % (folder.course.id, folder.id)).text)
            log.debug("Got response: %s" % response)

        documents = [Document.from_response(response, folder) for response in response["documents"]]

        folders = [Folder.from_response(response, folder) for response in response["folders"]]

        return documents + folders

Response: Returns a list of all contents of a folder. Returns a list of all Documents and Folders in this order.


Instruction:
Can you generate the documentation for the following Python 3 function
def download_document(self, document: Document, overwrite=True, path=None):
        if not path:
            path = os.path.join(os.path.expanduser(c["base_path"]), document.path)
        if (self.modified(document) and overwrite) or not os.path.exists(join(path, document.title)):
            log.info("Downloading %s" % join(path, document.title))
            file = self._get('/api/documents/%s/download' % document.id, stream=True)
            os.makedirs(path, exist_ok=True)
            with open(join(path, document.title), 'wb') as f:
                shutil.copyfileobj(file.raw, f)

Response: Download a document to the given path.


Instruction:
Can you tell what is the following Python 3 function doing
def get_semester_title(self, node: BaseNode):
        log.debug("Getting Semester Title for %s" % node.course.id)
        return self._get_semester_from_id(node.course.semester)

Response: get the semester title of a node


Instruction:
Make a summary of the following Python 3 code
def get_courses(self):
        log.info("Listing Courses...")
        courses = json.loads(self._get('/api/courses').text)["courses"]
        courses = [Course.from_response(course) for course in courses]
        log.debug("Courses: %s" % [str(entry) for entry in courses])
        return courses

Response: List all courses in the user s site


Instruction:
Write a Python 3 script for
minimizing a scalar function using Brent s method.

Response:
def _minimize_scalar(
        self, desc="Progress", rtol=1.4902e-08, atol=1.4902e-08, verbose=True
    ):
        """
        Minimize a scalar function using Brent's method.

        Parameters
        ----------
        verbose : bool
            ``True`` for verbose output; ``False`` otherwise.
        """
        from tqdm import tqdm
        from numpy import asarray
        from brent_search import minimize as brent_minimize

        variables = self._variables.select(fixed=False)
        if len(variables) != 1:
            raise ValueError("The number of variables must be equal to one.")

        var = variables[variables.names()[0]]
        progress = tqdm(desc=desc, disable=not verbose)

        def func(x):
            progress.update(1)
            var.value = x
            return self.__sign * self.value()

        r = asarray(
            brent_minimize(func, a=var.bounds[0], b=var.bounds[1], rtol=rtol, atol=atol)
        )
        var.value = r[0]
        progress.close()


Instruction:
Can you tell what is the following Python 3 function doing
def digest(self, alg='sha256', b64=True, strip=True):
        import base64, hashlib

        h = hashlib.new(alg)
        h.update(str(self).encode('utf-8'))
        if b64 == True:
            # this returns a string with a predictable amount of = padding at the end
            b = base64.urlsafe_b64encode(h.digest()).decode('ascii')
            if strip == True:
                b = b.rstrip('=')
            return b
        else:
            return h.hexdigest()

Response: Returns a url - safe base64 - encoded SHA - 256 hash of the string


Instruction:
How would you implement a function in Python 3 that
turns a string to CamelCase omitting non - word characters

Response:
def camelify(self):
        """turn a string to CamelCase, omitting non-word characters"""
        outstring = self.titleify(allwords=True)
        outstring = re.sub(r"&[^;]+;", " ", outstring)
        outstring = re.sub(r"\W+", "", outstring)
        return String(outstring)


Instruction:
Write a Python 3 script to
take a string and makes a title from it

Response:
def titleify(self, lang='en', allwords=False, lastword=True):
        """takes a string and makes a title from it"""
        if lang in LOWERCASE_WORDS:
            lc_words = LOWERCASE_WORDS[lang]
        else:
            lc_words = []
        s = str(self).strip()
        l = re.split(r"([_\W]+)", s)
        for i in range(len(l)):
            l[i] = l[i].lower()
            if (
                allwords == True
                or i == 0
                or (lastword == True and i == len(l) - 1)
                or l[i].lower() not in lc_words
            ):
                w = l[i]
                if len(w) > 1:
                    w = w[0].upper() + w[1:]
                else:
                    w = w.upper()
                l[i] = w
        s = "".join(l)
        return String(s)


Instruction:
How would you explain what the following Python 3 function does
def identifier(self, camelsplit=False, ascii=True):
        return self.nameify(camelsplit=camelsplit, ascii=ascii, sep='_')

Response: return a python identifier from the string


Instruction:
Implement a function in Python 3 to
return an XML name ( hyphen - separated by default initial underscore if non - letter

Response:
def nameify(self, camelsplit=False, ascii=True, sep='-'):
        """return an XML name (hyphen-separated by default, initial underscore if non-letter)"""
        s = String(str(self))  # immutable
        if camelsplit == True:
            s = s.camelsplit()
        s = s.hyphenify(ascii=ascii).replace('-', sep)
        if len(s) == 0 or re.match("[A-Za-z_]", s[0]) is None:
            s = "_" + s
        return String(s)


Instruction:
How would you explain what the following Python 3 function does
def hyphenify(self, ascii=False):
        s = str(self)
        s = re.sub("""['"\u2018\u2019\u201c\u201d]""", '', s)  # quotes
        s = re.sub(r'(?:\s|%20)+', '-', s)  # whitespace
        if ascii == True:  # ASCII-only
            s = s.encode('ascii', 'xmlcharrefreplace').decode('ascii')  # use entities
        s = re.sub("&?([^;]*?);", r'.\1-', s)  # entities
        s = s.replace('#', 'u')
        s = re.sub(r"\W+", '-', s).strip(' -')
        return String(s)

Response: Turn non - word characters into single hyphens.


Instruction:
How would you explain what the following Python 3 function does
def camelsplit(self):
        s = str(self)
        for i in range(len(s) - 1, -1, -1):
            if i != 0 and (
                (s[i].isupper() and s[i - 1].isalnum() and not s[i - 1].isupper())
                or (s[i].isnumeric() and s[i - 1].isalpha())
            ):
                s = s[:i] + ' ' + s[i:]
        return String(s.strip())

Response: Turn a CamelCase string into a string with spaces


Instruction:
Create a Python 3 function for
running the event scheduler.

Response:
def run(self):
        """Executed on startup of application"""
        self.api = self.context.get("cls")(self.context)
        self.context["inst"].append(self)  # Adapters used by strategies

        for call, calldata in self.context.get("calls", {}).items():
            def loop():
                """Loop on event scheduler, calling calls"""
                while not self.stopped.wait(calldata.get("delay", None)):
                    self.call(call, calldata.get("arguments", None))

            self.thread[call] = Process(target=loop)
            self.thread[call].start()


Instruction:
How would you explain what the following Python 3 function does
def call(self, callname, arguments=None):
        # See if a method override exists
        action = getattr(self.api, callname, None)
        if action is None:
            try:
                action = self.api.ENDPOINT_OVERRIDES.get(callname, None)
            except AttributeError:
                action = callname

        if not callable(action):
            request = self._generate_request(action, arguments)
            if action is None:
                return self._generate_result(
                    callname, self.api.call(*call_args(callname, arguments)))
            return self._generate_result(
                callname, self.api.call(*call_args(action, arguments)))

        request = self._generate_request(callname, arguments)
        return self._generate_result(callname, action(request))

Response: Executes on each scheduled iteration


Instruction:
Write a Python 3 script to
generate a request object for delivery to the API

Response:
def _generate_request(self, callname, request):
        """Generate a request object for delivery to the API"""
        # Retrieve path from API class
        schema = self.api.request_schema()
        schema.context['callname'] = callname
        return schema.dump(request).data.get("payload")


Instruction:
Can you tell what is the following Python 3 function doing
def _generate_result(self, callname, result):
        # Retrieve path from API class
        schema = self.api.result_schema()
        schema.context['callname'] = callname
        self.callback(schema.load(result), self.context)

Response: Generate a result object for delivery to the context object


Instruction:
Given the following Python 3 function, write the documentation
def excel_key(index):
    X = lambda n: ~n and X((n // 26)-1) + chr(65 + (n % 26)) or ''
    return X(int(index))

Response: create a key for index by converting index into base - 26 number using A - Z as the characters.


Instruction:
Can you implement a function in Python 3 that
converts a raw crash and processed crash to a crash data structure used by signature generation.

Response:
def convert_to_crash_data(raw_crash, processed_crash):
    """
    Takes a raw crash and a processed crash (these are Socorro-centric
    data structures) and converts them to a crash data structure used
    by signature generation.

    :arg raw_crash: raw crash data from Socorro
    :arg processed_crash: processed crash data from Socorro

    :returns: crash data structure that conforms to the schema

    """
    # We want to generate fresh signatures, so we remove the "normalized" field
    # from stack frames from the processed crash because this is essentially
    # cached data from previous processing
    for thread in glom(processed_crash, 'json_dump.threads', default=[]):
        for frame in thread.get('frames', []):
            if 'normalized' in frame:
                del frame['normalized']

    crash_data = {
        # JavaStackTrace or None
        'java_stack_trace': glom(processed_crash, 'java_stack_trace', default=None),

        # int or None
        'crashing_thread': glom(
            processed_crash, 'json_dump.crash_info.crashing_thread', default=None
        ),

        # list of CStackTrace or None
        'threads': glom(processed_crash, 'json_dump.threads', default=None),

        # int or None
        'hang_type': glom(processed_crash, 'hang_type', default=None),

        # text or None
        'os': glom(processed_crash, 'json_dump.system_info.os', default=None),

        # int or None
        'oom_allocation_size': int_or_none(glom(raw_crash, 'OOMAllocationSize', default=None)),

        # text or None
        'abort_message': glom(raw_crash, 'AbortMessage', default=None),

        # text or None
        'mdsw_status_string': glom(processed_crash, 'mdsw_status_string', default=None),

        # text json with "phase", "conditions" (complicated--see code) or None
        'async_shutdown_timeout': glom(raw_crash, 'AsyncShutdownTimeout', default=None),

        # text or None
        'jit_category': glom(processed_crash, 'classifications.jit.category', default=None),

        # text or None
        'ipc_channel_error': glom(raw_crash, 'ipc_channel_error', default=None),

        # text or None
        'ipc_message_name': glom(raw_crash, 'IPCMessageName', default=None),

        # text
        'moz_crash_reason': glom(processed_crash, 'moz_crash_reason', default=None),

        # text; comma-delimited e.g. "browser,flash1,flash2"
        'additional_minidumps': glom(raw_crash, 'additional_minidumps', default=''),

        # pull out the original signature if there was one
        'original_signature': glom(processed_crash, 'signature', default='')
    }
    return crash_data


Instruction:
Can you create a Python 3 function that
takes a text and drops all non - printable and non - ASCII characters and also any whitespace characters that aren t space.

Response:
def drop_bad_characters(text):
    """Takes a text and drops all non-printable and non-ascii characters and
    also any whitespace characters that aren't space.

    :arg str text: the text to fix

    :returns: text with all bad characters dropped

    """
    # Strip all non-ascii and non-printable characters
    text = ''.join([c for c in text if c in ALLOWED_CHARS])
    return text


Instruction:
Can you write a function in Python 3 where it
parses a source file thing and returns the file name

Response:
def parse_source_file(source_file):
    """Parses a source file thing and returns the file name

    Example:

    >>> parse_file('hg:hg.mozilla.org/releases/mozilla-esr52:js/src/jit/MIR.h:755067c14b06')
    'js/src/jit/MIR.h'

    :arg str source_file: the source file ("file") from a stack frame

    :returns: the filename or ``None`` if it couldn't determine one

    """
    if not source_file:
        return None

    vcsinfo = source_file.split(':')
    if len(vcsinfo) == 4:
        # These are repositories or cloud file systems (e.g. hg, git, s3)
        vcstype, root, vcs_source_file, revision = vcsinfo
        return vcs_source_file

    if len(vcsinfo) == 2:
        # These are directories on someone's Windows computer and vcstype is a
        # file system (e.g. "c:", "d:", "f:")
        vcstype, vcs_source_file = vcsinfo
        return vcs_source_file

    if source_file.startswith('/'):
        # These are directories on OSX or Linux
        return source_file

    # We have no idea what this is, so return None
    return None


Instruction:
Can you write a function in Python 3 where it
predicates for whether the open token is in an exception context

Response:
def _is_exception(exceptions, before_token, after_token, token):
    """Predicate for whether the open token is in an exception context

    :arg exceptions: list of strings or None
    :arg before_token: the text of the function up to the token delimiter
    :arg after_token: the text of the function after the token delimiter
    :arg token: the token (only if we're looking at a close delimiter

    :returns: bool

    """
    if not exceptions:
        return False
    for s in exceptions:
        if before_token.endswith(s):
            return True
        if s in token:
            return True
    return False


Instruction:
Implement a Python 3 function for
collapsing the text between two delimiters in a frame function value returning a new frame with the text collapsed.

Response:
def collapse(
    function,
    open_string,
    close_string,
    replacement='',
    exceptions=None,
):
    """Collapses the text between two delimiters in a frame function value

    This collapses the text between two delimiters and either removes the text
    altogether or replaces it with a replacement string.

    There are certain contexts in which we might not want to collapse the text
    between two delimiters. These are denoted as "exceptions" and collapse will
    check for those exception strings occuring before the token to be replaced
    or inside the token to be replaced.

    Before::

        IPC::ParamTraits<nsTSubstring<char> >::Write(IPC::Message *,nsTSubstring<char> const &)
               ^        ^ open token
               exception string occurring before open token

    Inside::

        <rayon_core::job::HeapJob<BODY> as rayon_core::job::Job>::execute
        ^                              ^^^^ exception string inside token
        open token

    :arg function: the function value from a frame to collapse tokens in
    :arg open_string: the open delimiter; e.g. ``(``
    :arg close_string: the close delimiter; e.g. ``)``
    :arg replacement: what to replace the token with; e.g. ``<T>``
    :arg exceptions: list of strings denoting exceptions where we don't want
        to collapse the token

    :returns: new function string with tokens collapsed

    """
    collapsed = []
    open_count = 0
    open_token = []

    for i, char in enumerate(function):
        if not open_count:
            if char == open_string and not _is_exception(exceptions, function[:i], function[i + 1:], ''):  # noqa
                open_count += 1
                open_token = [char]
            else:
                collapsed.append(char)

        else:
            if char == open_string:
                open_count += 1
                open_token.append(char)

            elif char == close_string:
                open_count -= 1
                open_token.append(char)

                if open_count == 0:
                    token = ''.join(open_token)
                    if _is_exception(exceptions, function[:i], function[i + 1:], token):
                        collapsed.append(''.join(open_token))
                    else:
                        collapsed.append(replacement)
                    open_token = []
            else:
                open_token.append(char)

    if open_count:
        token = ''.join(open_token)
        if _is_exception(exceptions, function[:i], function[i + 1:], token):
            collapsed.append(''.join(open_token))
        else:
            collapsed.append(replacement)

    return ''.join(collapsed)


Instruction:
Can you generate the documentation for the following Python 3 function
def drop_prefix_and_return_type(function):
    DELIMITERS = {
        '(': ')',
        '{': '}',
        '[': ']',
        '<': '>',
        '`': "'"
    }
    OPEN = DELIMITERS.keys()
    CLOSE = DELIMITERS.values()

    # The list of tokens accumulated so far
    tokens = []

    # Keeps track of open delimiters so we can match and close them
    levels = []

    # The current token we're building
    current = []

    for i, char in enumerate(function):
        if char in OPEN:
            levels.append(char)
            current.append(char)
        elif char in CLOSE:
            if levels and DELIMITERS[levels[-1]] == char:
                levels.pop()
                current.append(char)
            else:
                # This is an unmatched close.
                current.append(char)
        elif levels:
            current.append(char)
        elif char == ' ':
            tokens.append(''.join(current))
            current = []
        else:
            current.append(char)

    if current:
        tokens.append(''.join(current))

    while len(tokens) > 1 and tokens[-1].startswith(('(', '[clone')):
        # It's possible for the function signature to have a space between
        # the function name and the parenthesized arguments or [clone ...]
        # thing. If that's the case, we join the last two tokens. We keep doing
        # that until the last token is nice.
        #
        # Example:
        #
        #     somefunc (int arg1, int arg2)
        #             ^
        #     somefunc(int arg1, int arg2) [clone .cold.111]
        #                                 ^
        #     somefunc(int arg1, int arg2) [clone .cold.111] [clone .cold.222]
        #                                 ^                 ^
        tokens = tokens[:-2] + [' '.join(tokens[-2:])]

    return tokens[-1]

Response: Takes the function value from a frame and drops the prefix and return type of the function value.


Instruction:
Create a Python 3 function for
waiting for all the connections made and their handshakes to finish.

Response:
def wait_connected(self, conns=None, timeout=None):
        '''Wait for connections to be made and their handshakes to finish

        :param conns:
            a single or list of (host, port) tuples with the connections that
            must be finished before the method will return. defaults to all the
            peers the :class:`Hub` was instantiated with.
        :param timeout:
            maximum time to wait in seconds. with None, there is no timeout.
        :type timeout: float or None

        :returns:
            ``True`` if all connections were made, ``False`` one or more
            failed.
        '''
        if timeout:
            deadline = time.time() + timeout
        conns = conns or self._started_peers.keys()
        if not hasattr(conns, "__iter__"):
            conns = [conns]

        for peer_addr in conns:
            remaining = max(0, deadline - time.time()) if timeout else None
            if not self._started_peers[peer_addr].wait_connected(remaining):
                if timeout:
                    log.warn("connect wait timed out after %.2f seconds" %
                            timeout)
                return False
        return True


Instruction:
Write a Python 3 function for
closing all peer connections and stop listening for new ones

Response:
def shutdown(self):
        'Close all peer connections and stop listening for new ones'
        log.info("shutting down")

        for peer in self._dispatcher.peers.values():
            peer.go_down(reconnect=False)

        if self._listener_coro:
            backend.schedule_exception(
                    errors._BailOutOfListener(), self._listener_coro)
        if self._udp_listener_coro:
            backend.schedule_exception(
                    errors._BailOutOfListener(), self._udp_listener_coro)


Instruction:
Can you generate the documentation for the following Python 3 function
def accept_publish(
            self, service, mask, value, method, handler=None, schedule=False):
        '''Set a handler for incoming publish messages

        :param service: the incoming message must have this service
        :type service: anything hash-able
        :param mask:
            value to be bitwise-and'ed against the incoming id, the result of
            which must mask the 'value' param
        :type mask: int
        :param value:
            the result of `routing_id & mask` must match this in order to
            trigger the handler
        :type value: int
        :param method: the method name
        :type method: string
        :param handler:
            the function that will be called on incoming matching messages
        :type handler: callable
        :param schedule:
            whether to schedule a separate greenlet running ``handler`` for
            each matching message. default ``False``.
        :type schedule: bool

        :raises:
            - :class:`ImpossibleSubscription
              <junction.errors.ImpossibleSubscription>` if there is no routing
              ID which could possibly match the mask/value pair
            - :class:`OverlappingSubscription
              <junction.errors.OverlappingSubscription>` if a prior publish
              registration that overlaps with this one (there is a
              service/method/routing id that would match *both* this *and* a
              previously-made registration).
        '''
        # support @hub.accept_publish(serv, mask, val, meth) decorator usage
        if handler is None:
            return lambda h: self.accept_publish(
                    service, mask, value, method, h, schedule)

        log.info("accepting publishes%s %r" % (
                " scheduled" if schedule else "",
                (service, (mask, value), method),))

        self._dispatcher.add_local_subscription(const.MSG_TYPE_PUBLISH,
                service, mask, value, method, handler, schedule)

        return handler

Response: Set a handler for incoming publish messages.


Instruction:
Can you implement a function in Python 3 that
removes a publish subscription from a service.

Response:
def unsubscribe_publish(self, service, mask, value):
        '''Remove a publish subscription

        :param service: the service of the subscription to remove
        :type service: anything hash-able
        :param mask: the mask of the subscription to remove
        :type mask: int
        :param value: the value in the subscription to remove
        :type value: int

        :returns:
            a boolean indicating whether the subscription was there (True) and
            removed, or not (False)
        '''
        log.info("unsubscribing from publish %r" % (
                (service, (mask, value)),))
        return self._dispatcher.remove_local_subscription(
                const.MSG_TYPE_PUBLISH, service, mask, value)


Instruction:
Make a summary of the following Python 3 code
def publish(self, service, routing_id, method, args=None, kwargs=None,
            broadcast=False, udp=False):
        '''Send a 1-way message

        :param service: the service name (the routing top level)
        :type service: anything hash-able
        :param int routing_id:
            the id used for routing within the registered handlers of the
            service
        :param string method: the method name to call
        :param tuple args:
            The positional arguments to send along with the request. If the
            first positional argument is a generator object, the publish will
            be sent in chunks :ref:`(more info) <chunked-messages>`.
        :param dict kwargs: keyword arguments to send along with the request
        :param bool broadcast:
            if ``True``, send to every peer with a matching subscription.
        :param bool udp: deliver the message over UDP instead of the usual TCP

        :returns: None. use 'rpc' methods for requests with responses.

        :raises:
            :class:`Unroutable <junction.errors.Unroutable>` if no peers are
            registered to receive the message
        '''
        if udp:
            func = self._dispatcher.send_publish_udp
        else:
            func = self._dispatcher.send_publish
        if not func(None, service, routing_id, method,
                args or (), kwargs or {}, singular=not broadcast):
            raise errors.Unroutable()

Response: Send a 1 - way message to all peers.


Instruction:
Write a Python 3 function for
getting the number of peers that would handle a particular publish

Response:
def publish_receiver_count(self, service, routing_id):
        '''Get the number of peers that would handle a particular publish

        :param service: the service name
        :type service: anything hash-able
        :param routing_id: the id used for limiting the service handlers
        :type routing_id: int
        '''
        peers = len(list(self._dispatcher.find_peer_routes(
            const.MSG_TYPE_PUBLISH, service, routing_id)))
        if self._dispatcher.locally_handles(const.MSG_TYPE_PUBLISH,
                service, routing_id):
            return peers + 1
        return peers


Instruction:
Can you tell what is the following Python 3 function doing
def accept_rpc(self, service, mask, value, method,
            handler=None, schedule=True):
        '''Set a handler for incoming RPCs

        :param service: the incoming RPC must have this service
        :type service: anything hash-able
        :param mask:
            value to be bitwise-and'ed against the incoming id, the result of
            which must mask the 'value' param
        :type mask: int
        :param value:
            the result of `routing_id & mask` must match this in order to
            trigger the handler
        :type value: int
        :param method: the method name to trigger handler
        :type method: string
        :param handler:
            the function that will be called on incoming matching RPC requests
        :type handler: callable
        :param schedule:
            whether to schedule a separate greenlet running ``handler`` for
            each matching message. default ``True``.
        :type schedule: bool

        :raises:
            - :class:`ImpossibleSubscription
              <junction.errors.ImpossibleSubscription>` if there is no routing
              ID which could possibly match the mask/value pair
            - :class:`OverlappingSubscription
              <junction.errors.OverlappingSubscription>` if a prior rpc
              registration that overlaps with this one (there is a
              service/method/routing id that would match *both* this *and* a
              previously-made registration).
        '''
        # support @hub.accept_rpc(serv, mask, val, meth) decorator usage
        if handler is None:
            return lambda h: self.accept_rpc(
                    service, mask, value, method, h, schedule)

        log.info("accepting RPCs%s %r" % (
                " scheduled" if schedule else "",
                (service, (mask, value), method),))

        self._dispatcher.add_local_subscription(const.MSG_TYPE_RPC_REQUEST,
                service, mask, value, method, handler, schedule)

        return handler

Response: Set a handler for incoming RPCs with a routing id and a method.


Instruction:
Write a Python 3 function for
removing a rpc subscription :param service: the service of the subscription to remove :type service: anything hash-able :param mask: the mask of the subscription to remove :type mask: int :param value: the value in the subscription to remove :type value: int :param handler: the handler function of the subscription to remove :type handler: callable :returns: a boolean indicating whether the subscription was there (True) and removed, or not (False)

Response:
def unsubscribe_rpc(self, service, mask, value):
        '''Remove a rpc subscription

        :param service: the service of the subscription to remove
        :type service: anything hash-able
        :param mask: the mask of the subscription to remove
        :type mask: int
        :param value: the value in the subscription to remove
        :type value: int
        :param handler: the handler function of the subscription to remove
        :type handler: callable

        :returns:
            a boolean indicating whether the subscription was there (True) and
            removed, or not (False)
        '''
        log.info("unsubscribing from RPC %r" % ((service, (mask, value)),))
        return self._dispatcher.remove_local_subscription(
                const.MSG_TYPE_RPC_REQUEST, service, mask, value)


Instruction:
Make a summary of the following Python 3 code
def send_rpc(self, service, routing_id, method, args=None, kwargs=None,
            broadcast=False):
        '''Send out an RPC request

        :param service: the service name (the routing top level)
        :type service: anything hash-able
        :param routing_id:
            The id used for routing within the registered handlers of the
            service.
        :type routing_id: int
        :param method: the method name to call
        :type method: string
        :param args:
            The positional arguments to send along with the request. If the
            first argument is a generator, the request will be sent in chunks
            :ref:`(more info) <chunked-messages>`.
        :type args: tuple
        :param kwargs: keyword arguments to send along with the request
        :type kwargs: dict
        :param broadcast:
            if ``True``, send to every peer with a matching subscription
        :type broadcast: bool

        :returns:
            a :class:`RPC <junction.futures.RPC>` object representing the
            RPC and its future response.

        :raises:
            :class:`Unroutable <junction.errors.Unroutable>` if no peers are
            registered to receive the message
        '''
        rpc = self._dispatcher.send_rpc(service, routing_id, method,
                args or (), kwargs or {}, not broadcast)

        if not rpc:
            raise errors.Unroutable()

        return rpc

Response: Send out an RPC request to all peers that are subscribed to the current node.


Instruction:
Can you generate the documentation for the following Python 3 function
def rpc(self, service, routing_id, method, args=None, kwargs=None,
            timeout=None, broadcast=False):
        '''Send an RPC request and return the corresponding response

        This will block waiting until the response has been received.

        :param service: the service name (the routing top level)
        :type service: anything hash-able
        :param routing_id:
            The id used for routing within the registered handlers of the
            service.
        :type routing_id: int
        :param method: the method name to call
        :type method: string
        :param args:
            The positional arguments to send along with the request. If the
            first argument is a generator, the request will be sent in chunks
            :ref:`(more info) <chunked-messages>`.
        :type args: tuple
        :param kwargs: keyword arguments to send along with the request
        :type kwargs: dict
        :param timeout:
            maximum time to wait for a response in seconds. with None, there is
            no timeout.
        :type timeout: float or None
        :param broadcast:
            if ``True``, send to every peer with a matching subscription
        :type broadcast: bool

        :returns:
            a list of the objects returned by the RPC's targets. these could be
            of any serializable type.

        :raises:
            - :class:`Unroutable <junction.errors.Unroutable>` if no peers are
              registered to receive the message
            - :class:`WaitTimeout <junction.errors.WaitTimeout>` if a timeout
              was provided and it expires
        '''
        rpc = self.send_rpc(service, routing_id, method,
                args or (), kwargs or {}, broadcast)
        return rpc.get(timeout)

Response: Send an RPC request and return the corresponding response.


Instruction:
Can you implement a function in Python 3 that
gets the number of peers that would handle a particular RPC

Response:
def rpc_receiver_count(self, service, routing_id):
        '''Get the number of peers that would handle a particular RPC

        :param service: the service name
        :type service: anything hash-able
        :param routing_id:
            the id used for narrowing within the service handlers
        :type routing_id: int

        :returns:
            the integer number of peers that would receive the described RPC
        '''
        peers = len(list(self._dispatcher.find_peer_routes(
            const.MSG_TYPE_RPC_REQUEST, service, routing_id)))
        if self._dispatcher.locally_handles(const.MSG_TYPE_RPC_REQUEST,
                service, routing_id):
            return peers + 1
        return peers


Instruction:
Make a summary of the following Python 3 code
def start(self):
        "Start up the hub's server, and have it start initiating connections"
        log.info("starting")

        self._listener_coro = backend.greenlet(self._listener)
        self._udp_listener_coro = backend.greenlet(self._udp_listener)
        backend.schedule(self._listener_coro)
        backend.schedule(self._udp_listener_coro)

        for addr in self._peers:
            self.add_peer(addr)

Response: Start up the hub s server and have it start initiating connections


Instruction:
Write a Python 3 function for
building a connection to the Hub at a given ( host port ) address

Response:
def add_peer(self, peer_addr):
        "Build a connection to the Hub at a given ``(host, port)`` address"
        peer = connection.Peer(
                self._ident, self._dispatcher, peer_addr, backend.Socket())
        peer.start()
        self._started_peers[peer_addr] = peer


Instruction:
Can you generate the documentation for the following Python 3 function
def peers(self):
        "list of the (host, port) pairs of all connected peer Hubs"
        return [addr for (addr, peer) in self._dispatcher.peers.items()
                if peer.up]

Response: list of the ( host port ) pairs of all connected peer Hubs


Instruction:
Can you generate a brief explanation for the following Python 3 code
def main(argv=None):
    parser = argparse.ArgumentParser(description=DESCRIPTION, epilog=EPILOG)
    parser.add_argument(
        '-v', '--verbose', help='increase output verbosity', action='store_true'
    )
    parser.add_argument(
        '--format', help='specify output format: csv, text (default)'
    )
    parser.add_argument(
        '--different-only', dest='different', action='store_true',
        help='limit output to just the signatures that changed',
    )
    parser.add_argument(
        'crashids', metavar='crashid', nargs='*', help='crash id to generate signatures for'
    )

    if argv is None:
        args = parser.parse_args()
    else:
        args = parser.parse_args(argv)

    if args.format == 'csv':
        outputter = CSVOutput
    else:
        outputter = TextOutput

    api_token = os.environ.get('SOCORRO_API_TOKEN', '')

    generator = SignatureGenerator()
    if args.crashids:
        crashids_iterable = args.crashids
    elif not sys.stdin.isatty():
        # If a script is piping to this script, then isatty() returns False. If
        # there is no script piping to this script, then isatty() returns True
        # and if we do list(sys.stdin), it'll block waiting for input.
        crashids_iterable = list(sys.stdin)
    else:
        crashids_iterable = []

    if not crashids_iterable:
        parser.print_help()
        return 0

    with outputter() as out:
        for crash_id in crashids_iterable:
            crash_id = crash_id.strip()

            resp = fetch('/RawCrash/', crash_id, api_token)
            if resp.status_code == 404:
                out.warning('%s: does not exist.' % crash_id)
                continue
            if resp.status_code == 429:
                out.warning('API rate limit reached. %s' % resp.content)
                # FIXME(willkg): Maybe there's something better we could do here. Like maybe wait a
                # few minutes.
                return 1
            if resp.status_code == 500:
                out.warning('HTTP 500: %s' % resp.content)
                continue

            raw_crash = resp.json()

            # If there's an error in the raw crash, then something is wrong--probably with the API
            # token. So print that out and exit.
            if 'error' in raw_crash:
                out.warning('Error fetching raw crash: %s' % raw_crash['error'])
                return 1

            resp = fetch('/ProcessedCrash/', crash_id, api_token)
            if resp.status_code == 404:
                out.warning('%s: does not have processed crash.' % crash_id)
                continue
            if resp.status_code == 429:
                out.warning('API rate limit reached. %s' % resp.content)
                # FIXME(willkg): Maybe there's something better we could do here. Like maybe wait a
                # few minutes.
                return 1
            if resp.status_code == 500:
                out.warning('HTTP 500: %s' % resp.content)
                continue

            processed_crash = resp.json()

            # If there's an error in the processed crash, then something is wrong--probably with the
            # API token. So print that out and exit.
            if 'error' in processed_crash:
                out.warning('Error fetching processed crash: %s' % processed_crash['error'])
                return 1

            old_signature = processed_crash['signature']
            crash_data = convert_to_crash_data(raw_crash, processed_crash)

            result = generator.generate(crash_data)

            if not args.different or old_signature != result.signature:
                out.data(crash_id, old_signature, result, args.verbose)

Response: This function takes crash data via args and generates a Socorro signature for it.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def send_result(self, return_code, output, service_description='', time_stamp=0, specific_servers=None):
        '''
        Send result to the Skinken WS
        '''
        if time_stamp == 0:
            time_stamp = int(time.time())

        if specific_servers == None:
            specific_servers = self.servers
        else:
            specific_servers = set(self.servers).intersection(specific_servers)

        for server in specific_servers:
            post_data = {}
            post_data['time_stamp'] = time_stamp
            post_data['host_name'] = self.servers[server]['custom_fqdn']
            post_data['service_description'] = service_description
            post_data['return_code'] = return_code
            post_data['output'] = output

            if self.servers[server]['availability']:
                url = '%s://%s:%s%s' % (self.servers[server]['protocol'],
                                         self.servers[server]['host'],
                                         self.servers[server]['port'],
                                         self.servers[server]['uri'])

                auth = (self.servers[server]['username'],
                        self.servers[server]['password'])

                try:
                    response = requests.post(url,
                                             auth=auth,
                                             headers=self.http_headers,
                                             verify=self.servers[server]['verify'],
                                             timeout=self.servers[server]['timeout'],
                                             data=post_data)
                    if response.status_code == 400:
                        LOG.error("[ws_shinken][%s]: HTTP status: %s - The content of the WebService call is incorrect",
                                      server,
                                      response.status_code)
                    elif response.status_code == 401:
                        LOG.error("[ws_shinken][%s]: HTTP status: %s - You must provide an username and password",
                                      server,
                                      response.status_code)
                    elif response.status_code == 403:
                        LOG.error("[ws_shinken][%s]: HTTP status: %s - The username or password is wrong",
                                      server,
                                      response.status_code)
                    elif response.status_code != 200:
                        LOG.error("[ws_shinken][%s]: HTTP status: %s", server, response.status_code)
                except (requests.ConnectionError, requests.Timeout), error:
                    self.servers[server]['availability'] = False
                    LOG.error(error)
            else:
                LOG.error("[ws_shinken][%s]: Data not sent, server is unavailable", server)

            if self.servers[server]['availability'] == False and self.servers[server]['cache'] == True:
                self.servers[server]['csv'].writerow(post_data)
                LOG.info("[ws_shinken][%s]: Data cached", server)

Response: Send the result to Skinken WS


Instruction:
Explain what the following Python 3 code does
def close_cache(self):
        '''
        Close cache of WS Shinken
        '''
        # Close all WS_Shinken cache files
        for server in self.servers:
            if self.servers[server]['cache'] == True:
                self.servers[server]['file'].close()

Response: Close cache of WS Shinken


Instruction:
How would you explain what the following Python 3 function does
def prepare_dir(app, directory, delete=False):
    logger.info("Preparing output directories for jinjaapidoc.")
    if os.path.exists(directory):
        if delete:
            logger.debug("Deleting dir %s", directory)
            shutil.rmtree(directory)
            logger.debug("Creating dir %s", directory)
            os.mkdir(directory)
    else:
        logger.debug("Creating %s", directory)
        os.mkdir(directory)

Response: Create apidoc dir and delete contents if delete is True.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def makename(package, module):
    # Both package and module can be None/empty.
    assert package or module, "Specify either package or module"
    if package:
        name = package
        if module:
            name += '.' + module
    else:
        name = module
    return name

Response: Join package and module with a dot.


Instruction:
Write a Python 3 script for
writing the content of a file to a directory.

Response:
def write_file(app, name, text, dest, suffix, dryrun, force):
    """Write the output file for module/package <name>.

    :param app: the sphinx app
    :type app: :class:`sphinx.application.Sphinx`
    :param name: the file name without file extension
    :type name: :class:`str`
    :param text: the content of the file
    :type text: :class:`str`
    :param dest: the output directory
    :type dest: :class:`str`
    :param suffix: the file extension
    :type suffix: :class:`str`
    :param dryrun: If True, do not create any files, just log the potential location.
    :type dryrun: :class:`bool`
    :param force: Overwrite existing files
    :type force: :class:`bool`
    :returns: None
    :raises: None
    """
    fname = os.path.join(dest, '%s.%s' % (name, suffix))
    if dryrun:
        logger.info('Would create file %s.' % fname)
        return
    if not force and os.path.isfile(fname):
        logger.info('File %s already exists, skipping.' % fname)
    else:
        logger.info('Creating file %s.' % fname)
        f = open(fname, 'w')
        try:
            f.write(text)
            relpath = os.path.relpath(fname, start=app.env.srcdir)
            abspath = os.sep + relpath
            docpath = app.env.relfn2path(abspath)[0]
            docpath = docpath.rsplit(os.path.extsep, 1)[0]
            logger.debug('Adding document %s' % docpath)
            app.env.found_docs.add(docpath)
        finally:
            f.close()


Instruction:
Can you implement a function in Python 3 that
imports the given name and return the name obj parent mod_name

Response:
def import_name(app, name):
    """Import the given name and return name, obj, parent, mod_name

    :param name: name to import
    :type name: str
    :returns: the imported object or None
    :rtype: object | None
    :raises: None
    """
    try:
        logger.debug('Importing %r', name)
        name, obj = autosummary.import_by_name(name)[:2]
        logger.debug('Imported %s', obj)
        return obj
    except ImportError as e:
        logger.warn("Jinjapidoc failed to import %r: %s", name, e)


Instruction:
Can you generate a brief explanation for the following Python 3 code
def get_members(app, mod, typ, include_public=None):
    def include_here(x):
        """Return true if the member should be included in mod.

        A member will be included if it is declared in this module or package.
        If the `jinjaapidoc_include_from_all` option is `True` then the member
        can also be included if it is listed in `__all__`.

        :param x: The member
        :type x: A class, exception, or function.
        :returns: True if the member should be included in mod. False otherwise.
        :rtype: bool
        """
        return (x.__module__ == mod.__name__ or (include_from_all and x.__name__ in all_list))

    all_list = getattr(mod, '__all__', [])
    include_from_all = app.config.jinjaapi_include_from_all

    include_public = include_public or []
    tests = {'class': lambda x: inspect.isclass(x) and not issubclass(x, BaseException) and include_here(x),
             'function': lambda x: inspect.isfunction(x) and include_here(x),
             'exception': lambda x: inspect.isclass(x) and issubclass(x, BaseException) and include_here(x),
             'data': lambda x: not inspect.ismodule(x) and not inspect.isclass(x) and not inspect.isfunction(x),
             'members': lambda x: True}
    items = []
    for name in dir(mod):
        i = getattr(mod, name)
        inspect.ismodule(i)

        if tests.get(typ, lambda x: False)(i):
            items.append(name)
    public = [x for x in items
              if x in include_public or not x.startswith('_')]
    logger.debug('Got members of %s of type %s: public %s and %s', mod, typ, public, items)
    return public, items

Response: Return the members of the given type in the given module.


Instruction:
Can you implement a function in Python 3 that
gets all submodules of the given module

Response:
def _get_submodules(app, module):
    """Get all submodules for the given module/package

    :param app: the sphinx app
    :type app: :class:`sphinx.application.Sphinx`
    :param module: the module to query or module path
    :type module: module | str
    :returns: list of module names and boolean whether its a package
    :rtype: list
    :raises: TypeError
    """
    if inspect.ismodule(module):
        if hasattr(module, '__path__'):
            p = module.__path__
        else:
            return []
    elif isinstance(module, str):
        p = module
    else:
        raise TypeError("Only Module or String accepted. %s given." % type(module))
    logger.debug('Getting submodules of %s', p)
    submodules = [(name, ispkg) for loader, name, ispkg in pkgutil.iter_modules(p)]
    logger.debug('Found submodules of %s: %s', module, submodules)
    return submodules


Instruction:
Can you write a function in Python 3 where it
gets all submodules without packages for the given module

Response:
def get_submodules(app, module):
    """Get all submodules without packages for the given module/package

    :param app: the sphinx app
    :type app: :class:`sphinx.application.Sphinx`
    :param module: the module to query or module path
    :type module: module | str
    :returns: list of module names excluding packages
    :rtype: list
    :raises: TypeError
    """
    submodules = _get_submodules(app, module)
    return [name for name, ispkg in submodules if not ispkg]


Instruction:
Given the following Python 3 function, write the documentation
def get_subpackages(app, module):
    submodules = _get_submodules(app, module)
    return [name for name, ispkg in submodules if ispkg]

Response: Get all subpackages for the given module


Instruction:
Can you tell what is the following Python 3 function doing
def get_context(app, package, module, fullname):
    var = {'package': package,
           'module': module,
           'fullname': fullname}
    logger.debug('Creating context for: package %s, module %s, fullname %s', package, module, fullname)
    obj = import_name(app, fullname)
    if not obj:
        for k in ('subpkgs', 'submods', 'classes', 'allclasses',
                  'exceptions', 'allexceptions', 'functions', 'allfunctions',
                  'data', 'alldata', 'memebers'):
            var[k] = []
        return var

    var['subpkgs'] = get_subpackages(app, obj)
    var['submods'] = get_submodules(app, obj)
    var['classes'], var['allclasses'] = get_members(app, obj, 'class')
    var['exceptions'], var['allexceptions'] = get_members(app, obj, 'exception')
    var['functions'], var['allfunctions'] = get_members(app, obj, 'function')
    var['data'], var['alldata'] = get_members(app, obj, 'data')
    var['members'] = get_members(app, obj, 'members')
    logger.debug('Created context: %s', var)
    return var

Response: Return a dict with variables for template rendering


Instruction:
Create a Python 3 function for
creating the text of the file and write it to dest.

Response:
def create_module_file(app, env, package, module, dest, suffix, dryrun, force):
    """Build the text of the file and write the file.

    :param app: the sphinx app
    :type app: :class:`sphinx.application.Sphinx`
    :param env: the jinja environment for the templates
    :type env: :class:`jinja2.Environment`
    :param package: the package name
    :type package: :class:`str`
    :param module: the module name
    :type module: :class:`str`
    :param dest: the output directory
    :type dest: :class:`str`
    :param suffix: the file extension
    :type suffix: :class:`str`
    :param dryrun: If True, do not create any files, just log the potential location.
    :type dryrun: :class:`bool`
    :param force: Overwrite existing files
    :type force: :class:`bool`
    :returns: None
    :raises: None
    """
    logger.debug('Create module file: package %s, module %s', package, module)
    template_file = MODULE_TEMPLATE_NAME
    template = env.get_template(template_file)
    fn = makename(package, module)
    var = get_context(app, package, module, fn)
    var['ispkg'] = False
    rendered = template.render(var)
    write_file(app, makename(package, module), rendered, dest, suffix, dryrun, force)


Instruction:
Given the following Python 3 function, write the documentation
def create_package_file(app, env, root_package, sub_package, private,
                        dest, suffix, dryrun, force):
    """Build the text of the file and write the file.

    :param app: the sphinx app
    :type app: :class:`sphinx.application.Sphinx`
    :param env: the jinja environment for the templates
    :type env: :class:`jinja2.Environment`
    :param root_package: the parent package
    :type root_package: :class:`str`
    :param sub_package: the package name without root
    :type sub_package: :class:`str`
    :param private: Include \"_private\" modules
    :type private: :class:`bool`
    :param dest: the output directory
    :type dest: :class:`str`
    :param suffix: the file extension
    :type suffix: :class:`str`
    :param dryrun: If True, do not create any files, just log the potential location.
    :type dryrun: :class:`bool`
    :param force: Overwrite existing files
    :type force: :class:`bool`
    :returns: None
    :raises: None
    """
    logger.debug('Create package file: rootpackage %s, sub_package %s', root_package, sub_package)
    template_file = PACKAGE_TEMPLATE_NAME
    template = env.get_template(template_file)
    fn = makename(root_package, sub_package)
    var = get_context(app, root_package, sub_package, fn)
    var['ispkg'] = True
    for submod in var['submods']:
        if shall_skip(app, submod, private):
            continue
        create_module_file(app, env, fn, submod, dest, suffix, dryrun, force)
    rendered = template.render(var)
    write_file(app, fn, rendered, dest, suffix, dryrun, force)

Response: Create the text of the file and write it to dest.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def shall_skip(app, module, private):
    logger.debug('Testing if %s should be skipped.', module)
    # skip if it has a "private" name and this is selected
    if module != '__init__.py' and module.startswith('_') and \
       not private:
        logger.debug('Skip %s because its either private or __init__.', module)
        return True
    logger.debug('Do not skip %s', module)
    return False

Response: Check if we should skip this module.


Instruction:
Can you create a Python 3 function that
looks for every file in the directory tree and create the corresponding ReST files. :param app: the sphinx app :type app: :class:`sphinx.application.Sphinx` :param env: the jinja environment :type env: :class:`jinja2.Environment` :param src: the path to the python source files :type src: :class:`str` :param dest: the output directory :type dest: :class:`str` :param excludes: the paths to exclude :type excludes: :class:`list` :param followlinks: follow symbolic links :type followlinks: :class:`bool` :param force: overwrite existing files :type force: :class:`bool` :param dryrun: do not generate files :type dryrun: :class:`bool` :param private: include "_private" modules :type private: :class:`bool` :param suffix: the file extension :type suffix: :class:`str`

Response:
def recurse_tree(app, env, src, dest, excludes, followlinks, force, dryrun, private, suffix):
    """Look for every file in the directory tree and create the corresponding
    ReST files.

    :param app: the sphinx app
    :type app: :class:`sphinx.application.Sphinx`
    :param env: the jinja environment
    :type env: :class:`jinja2.Environment`
    :param src: the path to the python source files
    :type src: :class:`str`
    :param dest: the output directory
    :type dest: :class:`str`
    :param excludes: the paths to exclude
    :type excludes: :class:`list`
    :param followlinks: follow symbolic links
    :type followlinks: :class:`bool`
    :param force: overwrite existing files
    :type force: :class:`bool`
    :param dryrun: do not generate files
    :type dryrun: :class:`bool`
    :param private: include "_private" modules
    :type private: :class:`bool`
    :param suffix: the file extension
    :type suffix: :class:`str`
    """
    # check if the base directory is a package and get its name
    if INITPY in os.listdir(src):
        root_package = src.split(os.path.sep)[-1]
    else:
        # otherwise, the base is a directory with packages
        root_package = None

    toplevels = []
    for root, subs, files in walk(src, followlinks=followlinks):
        # document only Python module files (that aren't excluded)
        py_files = sorted(f for f in files
                          if os.path.splitext(f)[1] in PY_SUFFIXES and  # noqa: W504
                          not is_excluded(os.path.join(root, f), excludes))
        is_pkg = INITPY in py_files
        if is_pkg:
            py_files.remove(INITPY)
            py_files.insert(0, INITPY)
        elif root != src:
            # only accept non-package at toplevel
            del subs[:]
            continue
        # remove hidden ('.') and private ('_') directories, as well as
        # excluded dirs
        if private:
            exclude_prefixes = ('.',)
        else:
            exclude_prefixes = ('.', '_')
        subs[:] = sorted(sub for sub in subs if not sub.startswith(exclude_prefixes) and not
                         is_excluded(os.path.join(root, sub), excludes))
        if is_pkg:
            # we are in a package with something to document
            if subs or len(py_files) > 1 or not \
               shall_skip(app, os.path.join(root, INITPY), private):
                subpackage = root[len(src):].lstrip(os.path.sep).\
                    replace(os.path.sep, '.')
                create_package_file(app, env, root_package, subpackage,
                                    private, dest, suffix, dryrun, force)
                toplevels.append(makename(root_package, subpackage))
        else:
            # if we are at the root level, we don't require it to be a package
            assert root == src and root_package is None
            for py_file in py_files:
                if not shall_skip(app, os.path.join(src, py_file), private):
                    module = os.path.splitext(py_file)[0]
                    create_module_file(app, env, root_package, module, dest, suffix, dryrun, force)
                    toplevels.append(module)
    return toplevels


Instruction:
Can you generate a brief explanation for the following Python 3 code
def normalize_excludes(excludes):
    return [os.path.normpath(os.path.abspath(exclude)) for exclude in excludes]

Response: Normalize the excluded directory list.


Instruction:
Can you write a function in Python 3 where it
checks if the directory is in the exclude list.

Response:
def is_excluded(root, excludes):
    """Check if the directory is in the exclude list.

    Note: by having trailing slashes, we avoid common prefix issues, like
          e.g. an exlude "foo" also accidentally excluding "foobar".
    """
    root = os.path.normpath(root)
    for exclude in excludes:
        if root == exclude:
            return True
    return False


Instruction:
Explain what the following Python 3 code does
def generate(app, src, dest, exclude=[], followlinks=False,
             force=False, dryrun=False, private=False, suffix='rst',
             template_dirs=None):
    """Generage the rst files

    Raises an :class:`OSError` if the source path is not a directory.

    :param app: the sphinx app
    :type app: :class:`sphinx.application.Sphinx`
    :param src: path to python source files
    :type src: :class:`str`
    :param dest: output directory
    :type dest: :class:`str`
    :param exclude: list of paths to exclude
    :type exclude: :class:`list`
    :param followlinks: follow symbolic links
    :type followlinks: :class:`bool`
    :param force: overwrite existing files
    :type force: :class:`bool`
    :param dryrun: do not create any files
    :type dryrun: :class:`bool`
    :param private: include \"_private\" modules
    :type private: :class:`bool`
    :param suffix: file suffix
    :type suffix: :class:`str`
    :param template_dirs: directories to search for user templates
    :type template_dirs: None | :class:`list`
    :returns: None
    :rtype: None
    :raises: OSError
    """
    suffix = suffix.strip('.')
    if not os.path.isdir(src):
        raise OSError("%s is not a directory" % src)
    if not os.path.isdir(dest) and not dryrun:
        os.makedirs(dest)
    src = os.path.normpath(os.path.abspath(src))
    exclude = normalize_excludes(exclude)
    loader = make_loader(template_dirs)
    env = make_environment(loader)
    recurse_tree(app, env, src, dest, exclude, followlinks, force, dryrun, private, suffix)

Response: Generate the rst files for a single node.


Instruction:
Write a Python 3 script to
parse the config of the app and initiate the generation process :param app: the sphinx app :type app: :class:`sphinx.application.Sphinx` :returns: None :rtype: None :raises: None

Response:
def main(app):
    """Parse the config of the app and initiate the generation process

    :param app: the sphinx app
    :type app: :class:`sphinx.application.Sphinx`
    :returns: None
    :rtype: None
    :raises: None
    """
    c = app.config
    src = c.jinjaapi_srcdir

    if not src:
        return

    suffix = "rst"

    out = c.jinjaapi_outputdir or app.env.srcdir

    if c.jinjaapi_addsummarytemplate:
        tpath = pkg_resources.resource_filename(__package__, AUTOSUMMARYTEMPLATE_DIR)
        c.templates_path.append(tpath)

    tpath = pkg_resources.resource_filename(__package__, TEMPLATE_DIR)
    c.templates_path.append(tpath)

    prepare_dir(app, out, not c.jinjaapi_nodelete)
    generate(app, src, out,
             exclude=c.jinjaapi_exclude_paths,
             force=c.jinjaapi_force,
             followlinks=c.jinjaapi_followlinks,
             dryrun=c.jinjaapi_dryrun,
             private=c.jinjaapi_includeprivate,
             suffix=suffix,
             template_dirs=c.templates_path)


Instruction:
Here you have a function in Python 3, explain what it does
def _isclose(obja, objb, rtol=1e-05, atol=1e-08):
    return abs(obja - objb) <= (atol + rtol * abs(objb))

Response: Return floating point equality.


Instruction:
Implement a Python 3 function for
determining if an object is a real number.

Response:
def _isreal(obj):
    """
    Determine if an object is a real number.

    Both Python standard data types and Numpy data types are supported.

    :param obj: Object
    :type  obj: any

    :rtype: boolean
    """
    # pylint: disable=W0702
    if (obj is None) or isinstance(obj, bool):
        return False
    try:
        cond = (int(obj) == obj) or (float(obj) == obj)
    except:
        return False
    return cond


Instruction:
Explain what the following Python 3 code does
def _no_exp(number):
    if isinstance(number, bool) or (not isinstance(number, (int, float))):
        raise RuntimeError("Argument `number` is not valid")
    mant, exp = _to_scientific_tuple(number)
    if not exp:
        return str(number)
    floating_mant = "." in mant
    mant = mant.replace(".", "")
    if exp < 0:
        return "0." + "0" * (-exp - 1) + mant
    if not floating_mant:
        return mant + "0" * exp + (".0" if isinstance(number, float) else "")
    lfpart = len(mant) - 1
    if lfpart < exp:
        return (mant + "0" * (exp - lfpart)).rstrip(".")
    return mant

Response: r Converts a number to a string without using scientific notation.


Instruction:
Can you write a function in Python 3 where it
calculates the greatest common divisor of a list of numbers or a Numpy vector of numbers.

Response:
def gcd(vector):
    """
    Calculate the greatest common divisor (GCD) of a sequence of numbers.

    The sequence can be a list of numbers or a Numpy vector of numbers. The
    computations are carried out with a precision of 1E-12 if the objects are
    not `fractions <https://docs.python.org/3/library/fractions.html>`_. When
    possible it is best to use the `fractions
    <https://docs.python.org/3/library/fractions.html>`_ data type with the
    numerator and denominator arguments when computing the GCD of floating
    point numbers.

    :param vector: Vector of numbers
    :type  vector: list of numbers or Numpy vector of numbers
    """
    # pylint: disable=C1801
    if not len(vector):
        return None
    if len(vector) == 1:
        return vector[0]
    if len(vector) == 2:
        return pgcd(vector[0], vector[1])
    current_gcd = pgcd(vector[0], vector[1])
    for element in vector[2:]:
        current_gcd = pgcd(current_gcd, element)
    return current_gcd


Instruction:
How would you explain what the following Python 3 function does
def normalize(value, series, offset=0):
    if not _isreal(value):
        raise RuntimeError("Argument `value` is not valid")
    if not _isreal(offset):
        raise RuntimeError("Argument `offset` is not valid")
    try:
        smin = float(min(series))
        smax = float(max(series))
    except:
        raise RuntimeError("Argument `series` is not valid")
    value = float(value)
    offset = float(offset)
    if not 0 <= offset <= 1:
        raise ValueError("Argument `offset` has to be in the [0.0, 1.0] range")
    if not smin <= value <= smax:
        raise ValueError(
            "Argument `value` has to be within the bounds of argument `series`"
        )
    return offset + ((1.0 - offset) * (value - smin) / (smax - smin))

Response: r Scale a value to the range defined by a series.


Instruction:
How would you code a function in Python 3 to
calculate the greatest common divisor of two numbers.

Response:
def pgcd(numa, numb):
    """
    Calculate the greatest common divisor (GCD) of two numbers.

    :param numa: First number
    :type  numa: number

    :param numb: Second number
    :type  numb: number

    :rtype: number

    For example:

        >>> import pmisc, fractions
        >>> pmisc.pgcd(10, 15)
        5
        >>> str(pmisc.pgcd(0.05, 0.02))
        '0.01'
        >>> str(pmisc.pgcd(5/3.0, 2/3.0))[:6]
        '0.3333'
        >>> pmisc.pgcd(
        ...     fractions.Fraction(str(5/3.0)),
        ...     fractions.Fraction(str(2/3.0))
        ... )
        Fraction(1, 3)
        >>> pmisc.pgcd(
        ...     fractions.Fraction(5, 3),
        ...     fractions.Fraction(2, 3)
        ... )
        Fraction(1, 3)
    """
    # Test for integers this way to be valid also for Numpy data types without
    # actually importing (and package depending on) Numpy
    int_args = (int(numa) == numa) and (int(numb) == numb)
    fraction_args = isinstance(numa, Fraction) and isinstance(numb, Fraction)
    # Force conversion for Numpy data types
    if int_args:
        numa, numb = int(numa), int(numb)
    elif not fraction_args:
        numa, numb = float(numa), float(numb)
    # Limit floating numbers to a "sane" fractional part resolution
    if (not int_args) and (not fraction_args):
        numa, numb = (
            Fraction(_no_exp(numa)).limit_denominator(),
            Fraction(_no_exp(numb)).limit_denominator(),
        )
    while numb:
        numa, numb = (
            numb,
            (numa % numb if int_args else (numa % numb).limit_denominator()),
        )
    return int(numa) if int_args else (numa if fraction_args else float(numa))


Instruction:
Write a Python 3 function for
connecting to a websocket

Response:
def connect_ws(self, post_connect_callback, channels, reconnect=False):
        """
        Connect to a websocket
        :channels:  List of SockChannel instances
        """
        self.post_conn_cb = post_connect_callback
        self.channels = channels
        self.wsendpoint = self.context["conf"]["endpoints"].get("websocket")

        # Skip connecting if we don't have any channels to listen to
        if not channels:
            return

        # Create socket, connect, setting callbacks along the way
        self.sock = Socketcluster.socket(self.wsendpoint)
        self.sock.setBasicListener(self._on_connect, self._on_connect_close,
                                   self._on_connect_error)
        self.sock.setAuthenticationListener(self._on_set_auth, self._on_auth)
        self.sock.setreconnection(reconnect)
        self.sock.connect()


Instruction:
Can you tell what is the following Python 3 function doing
def wscall(self, method, query=None, callback=None):
        if callback is None:
            self.sock.emit(method, query)
        else:
            self.sock.emitack(method, query, callback)

Response: Submit a request on the websocket


Instruction:
Can you write a function in Python 3 where it
connects the provided channels to the socket.

Response:
def connect_channels(self, channels):
        """Connect the provided channels"""
        self.log.info(f"Connecting to channels...")
        for chan in channels:
            chan.connect(self.sock)
            self.log.info(f"\t{chan.channel}")


Instruction:
Write a Python 3 function that can
call when the client wants to set the auth token from the websocket

Response:
def _on_set_auth(self, sock, token):
        """Set Auth request received from websocket"""
        self.log.info(f"Token received: {token}")
        sock.setAuthtoken(token)


Instruction:
Here you have a function in Python 3, explain what it does
def _on_auth(self, sock, authenticated):  # pylint: disable=unused-argument
        def ack(eventname, error, data):  # pylint: disable=unused-argument
            """Ack"""
            if error:
                self.log.error(f"""OnAuth: {error}""")
            else:
                self.connect_channels(self.channels)
                self.post_conn_cb()

        sock.emitack("auth", self.creds, ack)

Response: Called when the client is listening and the client is authenticated.


Instruction:
Can you generate the documentation for the following Python 3 function
def _on_connect_error(self, sock, err):  # pylint: disable=unused-argument
        if isinstance(err, SystemExit):
            self.log.error(f"Shutting down websocket connection")
        else:
            self.log.error(f"Websocket error: {err}")

Response: Called when a websocket connection error occurs.


Instruction:
Can you tell what is the following Python 3 function doing
def connect(self, sock):
        def cbwrap(*args, **kwargs):
            """Callback wrapper; passes in response_type"""
            self.callback(self.response_type, *args, **kwargs)

        self.sock = sock
        self.sock.subscribe(self.channel)
        self.sock.onchannel(self.channel, cbwrap)

Response: Attach a given socket to a channel


Instruction:
Explain what the following Python 3 code does
def run_cmd(cmd, input=None, timeout=30, max_try=3, num_try=1):
    '''Run command `cmd`.

    It's like that, and that's the way it is.
    '''
    if type(cmd) == str:
        cmd = cmd.split()
    process = subprocess.Popen(cmd,
                               stdin=open('/dev/null', 'r'),
                               stdout=subprocess.PIPE,
                               stderr=subprocess.PIPE)
    communicate_has_timeout = func_has_arg(func=process.communicate,
                                           arg='timeout')
    exception = Exception
    if communicate_has_timeout:
        exception = subprocess.TimeoutExpired  # python 3.x
    stdout = stderr = b''
    exitcode = None
    try:
        if communicate_has_timeout:
            # python 3.x
            stdout, stderr = process.communicate(input, timeout)
            exitcode = process.wait()
        else:
            # python 2.x
            if timeout is None:
                stdout, stderr = process.communicate(input)
                exitcode = process.wait()
            else:
                # thread-recipe: https://stackoverflow.com/a/4825933
                def target():
                    # closure-recipe: https://stackoverflow.com/a/23558809
                    target.out, target.err = process.communicate(input)
                import threading
                thread = threading.Thread(target=target)
                thread.start()
                thread.join(timeout)
                if thread.is_alive():
                    process.terminate()
                    thread.join()
                    exitcode = None
                else:
                    exitcode = process.wait()
                stdout = target.out
                stderr = target.err
    except exception:
        if num_try < max_try:
            return run_cmd(cmd, input, timeout, max_try, num_try+1)
        else:
            return CmdResult(exitcode, stdout, stderr, cmd, input)
    return CmdResult(exitcode, stdout, stderr, cmd, input)

Response:
Run command `cmd`.

    It's like that, and that's the way it is.


Instruction:
Given the following Python 3 function, write the documentation
def pop_first_arg(argv):
    for arg in argv:
        if not arg.startswith('-'):
            argv.remove(arg)
            return (arg, argv)

    return (None, argv)

Response: pop first positional arg from the list of arguments


Instruction:
Can you generate the documentation for the following Python 3 function
def check_options(options, parser):
    if not options.get('release_environment', None):
        print("release environment is required")
        parser.print_help()
        return os.EX_USAGE

    return 0

Response: check options requirements print and return exit value


Instruction:
Can you create a Python 3 function that
writes all needed state info to filesystem

Response:
def write(self):
        """ write all needed state info to filesystem """
        dumped = self._fax.codec.dump(self.__state, open(self.state_file, 'w'))


Instruction:
Given the following Python 3 function, write the documentation
def package_config(path, template='__config__.ini.TEMPLATE', config_name='__config__.ini', **params):
    config_fns = []
    template_fns = rglob(path, template)
    for template_fn in template_fns:
        config_template = ConfigTemplate(fn=template_fn)
        config = config_template.render(
            fn=os.path.join(os.path.dirname(template_fn), config_name),
            prompt=True, path=path, **params)
        config.write()
        config_fns.append(config.fn)
        log.info('wrote %r' % config)
    return config_fns

Response: package the config file at the given path with a config template and file.


Instruction:
Write a Python 3 script for
writing the contents of this config to fn or its __filename__.

Response:
def write(self, fn=None, sorted=False, wait=0):
        """write the contents of this config to fn or its __filename__.
        """
        config = ConfigParser(interpolation=None)
        if sorted==True: keys.sort()
        for key in self.__dict__.get('ordered_keys') or self.keys():
            config[key] = {}
            ks = self[key].keys()
            if sorted==True: ks.sort()
            for k in ks:
                if type(self[key][k])==list and self.__join_list__ is not None:
                    config[key][k] = self.__join_list__.join([v for v in self[key][k] if v!=''])
                else:
                    config[key][k] = str(self[key][k])
        fn = fn or self.__dict__.get('__filename__')
        # use advisory locking on this file
        i = 0
        while os.path.exists(fn+'.LOCK') and i < wait:
            i += 1
            time.sleep(1)
        if os.path.exists(fn+'.LOCK'):
            raise FileExistsError(fn + ' is locked for writing')
        else:
            with open(fn+'.LOCK', 'w') as lf:
                lf.write(time.strftime("%Y-%m-%d %H:%M:%S %Z"))
            with open(fn, 'w') as f:
                config.write(f)
            os.remove(fn+'.LOCK')


Instruction:
Given the following Python 3 function, write the documentation
def expected_param_keys(self):
        expected_keys = []
        r = re.compile('%\(([^\)]+)\)s')
        for block in self.keys():
            for key in self[block].keys():
                s = self[block][key]
                if type(s)!=str: continue
                md = re.search(r, s)
                while md is not None:
                    k = md.group(1)
                    if k not in expected_keys:
                        expected_keys.append(k)
                    s = s[md.span()[1]:]
                    md = re.search(r, s)
        return expected_keys

Response: returns a list of params that this ConfigTemplate expects to receive


Instruction:
Can you generate the documentation for the following Python 3 function
def render(self, fn=None, prompt=False, **params):
        from getpass import getpass
        expected_keys = self.expected_param_keys()
        compiled_params = Dict(**params)
        for key in expected_keys:
            if key not in compiled_params.keys():
                if prompt==True:
                    if key=='password':
                        compiled_params[key] = getpass("%s: " % key)
                    else:
                        compiled_params[key] = input("%s: " % key)
                        if 'path' in key:
                            compiled_params[key] = compiled_params[key].replace('\\','')
                else:
                    compiled_params[key] = "%%(%s)s" % key

        config = ConfigTemplate(fn=fn, **self)
        config.__dict__['ordered_keys'] = self.__dict__.get('ordered_keys')
        for block in config.keys():
            for key in config[block].keys():
                if type(config[block][key])==str:
                    config[block][key] = config[block][key] % compiled_params
        return config

Response: Returns a Config object with the given params formatted via str. format


Instruction:
Can you create a Python 3 function that
takes a crash id pulls down data from Socorro generates signature data

Response:
def main():
    """Takes a crash id, pulls down data from Socorro, generates signature data"""
    parser = argparse.ArgumentParser(
        formatter_class=WrappedTextHelpFormatter,
        description=DESCRIPTION
    )
    parser.add_argument(
        '-v', '--verbose', help='increase output verbosity', action='store_true'
    )
    parser.add_argument(
        'crashid', help='crash id to generate signatures for'
    )

    args = parser.parse_args()

    api_token = os.environ.get('SOCORRO_API_TOKEN', '')

    crash_id = args.crashid.strip()

    resp = fetch('/RawCrash/', crash_id, api_token)
    if resp.status_code == 404:
        printerr('%s: does not exist.' % crash_id)
        return 1
    if resp.status_code == 429:
        printerr('API rate limit reached. %s' % resp.content)
        # FIXME(willkg): Maybe there's something better we could do here. Like maybe wait a
        # few minutes.
        return 1
    if resp.status_code == 500:
        printerr('HTTP 500: %s' % resp.content)
        return 1

    raw_crash = resp.json()

    # If there's an error in the raw crash, then something is wrong--probably with the API
    # token. So print that out and exit.
    if 'error' in raw_crash:
        print('Error fetching raw crash: %s' % raw_crash['error'], file=sys.stderr)
        return 1

    resp = fetch('/ProcessedCrash/', crash_id, api_token)
    if resp.status_code == 404:
        printerr('%s: does not have processed crash.' % crash_id)
        return 1
    if resp.status_code == 429:
        printerr('API rate limit reached. %s' % resp.content)
        # FIXME(willkg): Maybe there's something better we could do here. Like maybe wait a
        # few minutes.
        return 1
    if resp.status_code == 500:
        printerr('HTTP 500: %s' % resp.content)
        return 1

    processed_crash = resp.json()

    # If there's an error in the processed crash, then something is wrong--probably with the
    # API token. So print that out and exit.
    if 'error' in processed_crash:
        printerr('Error fetching processed crash: %s' % processed_crash['error'])
        return 1

    crash_data = convert_to_crash_data(raw_crash, processed_crash)

    print(json.dumps(crash_data, indent=2))