Instruction:
Can you write a function in Python 3 where it
adds an entry to a controlled vocabulary.

Response:
def add_cv_entry(self, cv_id, cve_id, values, ext_ref=None):
        """Add an entry to a controlled vocabulary.

        :param str cv_id: Name of the controlled vocabulary to add an entry.
        :param str cve_id: Name of the entry.
        :param list values: List of values of the form:
            ``(value, lang_ref, description)`` where description can be
            ``None``.
        :param str ext_ref: External reference.
        :throws KeyError: If there is no controlled vocabulary with that id.
        :throws ValueError: If a language in one of the entries doesn't exist.
        """
        for value, lang_ref, description in values:
            if lang_ref not in self.languages:
                raise ValueError('Language not present: {}'.format(lang_ref))
        self.controlled_vocabularies[cv_id][1][cve_id] = (values, ext_ref)


Instruction:
Can you generate a brief explanation for the following Python 3 code
def add_cv_description(self, cv_id, lang_ref, description=None):
        if lang_ref not in self.languages:
            raise ValueError('Language not present: {}'.format(lang_ref))
        self.controlled_vocabularies[cv_id][0].append((lang_ref, description))

Response: Adds a description to a controlled vocabulary.


Instruction:
Write a Python 3 function that can
add an external reference.

Response:
def add_external_ref(self, eid, etype, value):
        """Add an external reference.

        :param str eid: Name of the external reference.
        :param str etype: Type of the external reference, has to be in
            ``['iso12620', 'ecv', 'cve_id', 'lexen_id', 'resource_url']``.
        :param str value: Value of the external reference.
        :throws KeyError: if etype is not in the list of possible types.
        """
        if etype not in self.ETYPES:
            raise KeyError('etype not in {}'.format(self.ETYPES))
        self.external_refs[eid] = (etype, value)


Instruction:
Write a Python 3 script for
adding a language. :param str lang_id: ID of the language. :param str lang_def: Definition of the language(preferably ISO-639-3). :param str lang_label: Label of the language.

Response:
def add_language(self, lang_id, lang_def=None, lang_label=None):
        """Add a language.

        :param str lang_id: ID of the language.
        :param str lang_def: Definition of the language(preferably ISO-639-3).
        :param str lang_label: Label of the language.
        """
        self.languages[lang_id] = (lang_def, lang_label)


Instruction:
How would you implement a function in Python 3 that
adds a new lexicon reference.

Response:
def add_lexicon_ref(self, lrid, name, lrtype, url, lexicon_id,
                        lexicon_name, datcat_id=None, datcat_name=None):
        """Add lexicon reference.

        :param str lrid: Lexicon reference internal ID.
        :param str name: Lexicon reference display name.
        :param str lrtype: Lexicon reference service type.
        :param str url: Lexicon reference service location
        :param str lexicon_id: Lexicon reference service id.
        :param str lexicon_name: Lexicon reference service name.
        :param str datacat_id: Lexicon reference identifier of data category.
        :param str datacat_name: Lexicon reference name of data category.
        """
        self.lexicon_refs[lrid] = {
            'LEX_REF_ID': lrid,
            'NAME': name,
            'TYPE': lrtype,
            'URL': url,
            'LEXICON_ID': lexicon_id,
            'LEXICON_NAME': lexicon_name,
            'DATCAT_ID': datcat_id,
            'DATCAT_NAME': datcat_name
            }


Instruction:
Can you create a Python 3 function that
adds a linguistic type to the list of linguistic types in the resource.

Response:
def add_linguistic_type(self, lingtype, constraints=None,
                            timealignable=True, graphicreferences=False,
                            extref=None, param_dict=None):
        """Add a linguistic type.

        :param str lingtype: Name of the linguistic type.
        :param str constraints: Constraint name.
        :param bool timealignable: Flag for time alignable.
        :param bool graphicreferences: Flag for graphic references.
        :param str extref: External reference.
        :param dict param_dict: TAG attributes, when this is not ``None`` it
                                 will ignore all other options. Please only use
                                 dictionaries coming from the
                                 :func:`get_parameters_for_linguistic_type`
        :raises KeyError: If a constraint is not defined
        """
        if param_dict:
            self.linguistic_types[lingtype] = param_dict
        else:
            if constraints:
                self.constraints[constraints]
            self.linguistic_types[lingtype] = {
                'LINGUISTIC_TYPE_ID': lingtype,
                'TIME_ALIGNABLE': str(timealignable).lower(),
                'GRAPHIC_REFERENCES': str(graphicreferences).lower(),
                'CONSTRAINTS': constraints}
            if extref is not None:
                self.linguistic_types[lingtype]['EXT_REF'] = extref


Instruction:
Given the following Python 3 function, write the documentation
def add_linked_file(self, file_path, relpath=None, mimetype=None,
                        time_origin=None, ex_from=None):
        """Add a linked file.

        :param str file_path: Path of the file.
        :param str relpath: Relative path of the file.
        :param str mimetype: Mimetype of the file, if ``None`` it tries to
            guess it according to the file extension which currently only works
            for wav, mpg, mpeg and xml.
        :param int time_origin: Time origin for the media file.
        :param str ex_from: Extracted from field.
        :raises KeyError: If mimetype had to be guessed and a non standard
                          extension or an unknown mimetype.
        """
        if mimetype is None:
            mimetype = self.MIMES[file_path.split('.')[-1]]
        self.media_descriptors.append({
            'MEDIA_URL': file_path, 'RELATIVE_MEDIA_URL': relpath,
            'MIME_TYPE': mimetype, 'TIME_ORIGIN': time_origin,
            'EXTRACTED_FROM': ex_from})

Response: Adds a linked file to the media_descriptors list.


Instruction:
Make a summary of the following Python 3 code
def add_locale(self, language_code, country_code=None, variant=None):
        self.locales[language_code] = (country_code, variant)

Response: Add a locale to the locale table.


Instruction:
Explain what the following Python 3 code does
def add_ref_annotation(self, id_tier, tier2, time, value='',
                           prev=None, svg=None):
        """Add a reference annotation.
        .. note:: When a timepoint matches two annotations the new reference
        annotation will reference to the first annotation. To circumvent this
        it's always safer to take the middle of the annotation you want to
        reference to.

        :param str id_tier: Name of the tier.
        :param str tier2: Tier of the referenced annotation.
        :param int time: Time of the referenced annotation.
        :param str value: Value of the annotation.
        :param str prev: Id of the previous annotation.
        :param str svg_ref: Svg reference.
        :raises KeyError: If the tier is non existent.
        :raises ValueError: If the tier already contains normal annotations or
            if there is no annotation in the tier on the time to reference to.
        """
        if self.tiers[id_tier][0]:
            raise ValueError('This tier already contains normal annotations.')
        ann = None
        for aid, (begin, end, _, _) in self.tiers[tier2][0].items():
            begin = self.timeslots[begin]
            end = self.timeslots[end]
            if begin <= time and end >= time:
                ann = aid
                break
        if not ann:
            raise ValueError('There is no annotation to reference to.')
        aid = self.generate_annotation_id()
        self.annotations[aid] = id_tier
        self.tiers[id_tier][1][aid] = (ann, value, prev, svg)

Response: Add a reference annotation to the given timepoint.


Instruction:
Write a Python 3 function that can
add a secondary linked file.

Response:
def add_secondary_linked_file(self, file_path, relpath=None, mimetype=None,
                                  time_origin=None, assoc_with=None):
        """Add a secondary linked file.

        :param str file_path: Path of the file.
        :param str relpath: Relative path of the file.
        :param str mimetype: Mimetype of the file, if ``None`` it tries to
            guess it according to the file extension which currently only works
            for wav, mpg, mpeg and xml.
        :param int time_origin: Time origin for the media file.
        :param str assoc_with: Associated with field.
        :raises KeyError: If mimetype had to be guessed and a non standard
                          extension or an unknown mimetype.
        """
        if mimetype is None:
            mimetype = self.MIMES[file_path.split('.')[-1]]
        self.linked_file_descriptors.append({
            'LINK_URL': file_path, 'RELATIVE_LINK_URL': relpath,
            'MIME_TYPE': mimetype, 'TIME_ORIGIN': time_origin,
            'ASSOCIATED_WITH': assoc_with})


Instruction:
Can you generate the documentation for the following Python 3 function
def add_tier(self, tier_id, ling='default-lt', parent=None, locale=None,
                 part=None, ann=None, language=None, tier_dict=None):
        """Add a tier. When no linguistic type is given and the default
        linguistic type is unavailable then the assigned linguistic type will
        be the first in the list.

        :param str tier_id: Name of the tier.
        :param str ling: Linguistic type, if the type is not available it will
                         warn and pick the first available type.
        :param str parent: Parent tier name.
        :param str locale: Locale, if the locale is not present this option is
            ignored and the locale will not be set.
        :param str part: Participant.
        :param str ann: Annotator.
        :param str language: Language , if the language is not present this
            option is ignored and the language will not be set.
        :param dict tier_dict: TAG attributes, when this is not ``None`` it
                               will ignore all other options. Please only use
                               dictionaries coming from the
                               :func:`get_parameters_for_tier`
        :raises ValueError: If the tier_id is empty
        """
        if not tier_id:
            raise ValueError('Tier id is empty...')
        if ling not in self.linguistic_types:
            ling = sorted(self.linguistic_types.keys())[0]
        if locale and locale not in self.locales:
            locale = None
        if language and language not in self.languages:
            language = None
        if tier_dict is None:
            self.tiers[tier_id] = ({}, {}, {
                'TIER_ID': tier_id,
                'LINGUISTIC_TYPE_REF': ling,
                'PARENT_REF': parent,
                'PARTICIPANT': part,
                'DEFAULT_LOCALE': locale,
                'LANG_REF': language,
                'ANNOTATOR': ann}, len(self.tiers))
        else:
            self.tiers[tier_id] = ({}, {}, tier_dict, len(self.tiers))

Response: Add a tier to the list of available linguistics.


Instruction:
Can you tell what is the following Python 3 function doing
def clean_time_slots(self):
        ts = ((a[0], a[1]) for t in self.tiers.values() for a in t[0].values())
        for a in {a for b in ts for a in b} ^ set(self.timeslots):
            del(self.timeslots[a])

Response: Clean up all unused timeslots.


Instruction:
Can you generate the documentation for the following Python 3 function
def copy_tier(self, eaf_obj, tier_name):
        if tier_name in eaf_obj.get_tier_names():
            eaf_obj.remove_tier(tier_name)
        eaf_obj.add_tier(tier_name,
                         tier_dict=self.get_parameters_for_tier(tier_name))
        for ann in self.get_annotation_data_for_tier(tier_name):
            eaf_obj.insert_annotation(tier_name, ann[0], ann[1], ann[2])

Response: Copies a tier to another.


Instruction:
Here you have a function in Python 3, explain what it does
def create_gaps_and_overlaps_tier(self, tier1, tier2, tier_name=None,
                                      maxlen=-1, fast=False):
        """Create a tier with the gaps and overlaps of the annotations.
        For types see :func:`get_gaps_and_overlaps`

        :param str tier1: Name of the first tier.
        :param str tier2: Name of the second tier.
        :param str tier_name: Name of the new tier, if ``None`` the name will
                              be generated.
        :param int maxlen: Maximum length of gaps (skip longer ones), if ``-1``
                           no maximum will be used.
        :param bool fast: Flag for using the fast method.
        :returns: List of gaps and overlaps of the form:
                  ``[(type, start, end)]``.
        :raises KeyError: If a tier is non existent.
        :raises IndexError: If no annotations are available in the tiers.
        """
        if tier_name is None:
            tier_name = '{}_{}_ftos'.format(tier1, tier2)
        self.add_tier(tier_name)
        ftos = []
        ftogen = self.get_gaps_and_overlaps2(tier1, tier2, maxlen) if fast\
            else self.get_gaps_and_overlaps(tier1, tier2, maxlen)
        for fto in ftogen:
            ftos.append(fto)
            if fto[1]-fto[0] >= 1:
                self.add_annotation(tier_name, fto[0], fto[1], fto[2])
        self.clean_time_slots()
        return ftos

Response: Create a new tier with the gaps and overlaps of the annotations.


Instruction:
Can you generate the documentation for the following Python 3 function
def extract(self, start, end):
        from copy import deepcopy
        eaf_out = deepcopy(self)
        for t in eaf_out.get_tier_names():
            for ab, ae, value in eaf_out.get_annotation_data_for_tier(t):
                if ab > end or ae < start:
                    eaf_out.remove_annotation(t, (start-end)//2, False)
        eaf_out.clean_time_slots()
        return eaf_out

Response: Extracts the selected time frame as a new object.


Instruction:
Given the following Python 3 function, write the documentation
def filter_annotations(self, tier, tier_name=None, filtin=None,
                           filtex=None, regex=False, safe=False):
        """Filter annotations in a tier using an exclusive and/or inclusive
        filter.

        :param str tier: Name of the tier.
        :param str tier_name: Name of the output tier, when ``None`` the name
            will be generated.
        :param list filtin: List of strings to be included, if None all
            annotations all is included.
        :param list filtex: List of strings to be excluded, if None no strings
            are excluded.
        :param bool regex: If this flag is set, the filters are seen as regex
            matches.
        :param bool safe: Ignore zero length annotations(when working with
            possible malformed data).
        :returns: Name of the created tier.
        :raises KeyError: If the tier is non existent.
        """
        if tier_name is None:
            tier_name = '{}_filter'.format(tier)
        self.add_tier(tier_name)
        func = (lambda x, y: re.match(x, y)) if regex else lambda x, y: x == y
        for begin, end, value in self.get_annotation_data_for_tier(tier):
            if (filtin and not any(func(f, value) for f in filtin)) or\
                    (filtex and any(func(f, value) for f in filtex)):
                continue
            if not safe or end > begin:
                self.add_annotation(tier_name, begin, end, value)
        self.clean_time_slots()
        return tier_name

Response: Filter the annotations in a tier using an exclusive and or inclusive filter.


Instruction:
Given the following Python 3 function, write the documentation
def generate_annotation_id(self):
        if not self.maxaid:
            valid_anns = [int(''.join(filter(str.isdigit, a)))
                          for a in self.timeslots]
            self.maxaid = max(valid_anns + [1])+1
        else:
            self.maxaid += 1
        return 'a{:d}'.format(self.maxaid)

Response:
Generate the next annotation id. This function is mainly used
        internally.


Instruction:
How would you code a function in Python 3 to
generate the next timeslot id, this function is mainly used internally :param int time: Initial time to assign to the timeslot. :raises ValueError: If the time is negative.

Response:
def generate_ts_id(self, time=None):
        """Generate the next timeslot id, this function is mainly used
        internally

        :param int time: Initial time to assign to the timeslot.
        :raises ValueError: If the time is negative.
        """
        if time and time < 0:
            raise ValueError('Time is negative...')
        if not self.maxts:
            valid_ts = [int(''.join(filter(str.isdigit, a)))
                        for a in self.timeslots]
            self.maxts = max(valid_ts + [1])+1
        else:
            self.maxts += 1
        ts = 'ts{:d}'.format(self.maxts)
        self.timeslots[ts] = time
        return ts


Instruction:
How would you explain what the following Python 3 function does
def get_annotation_data_at_time(self, id_tier, time):
        if self.tiers[id_tier][1]:
            return self.get_ref_annotation_at_time(id_tier, time)
        anns = self.tiers[id_tier][0]
        return sorted([(self.timeslots[m[0]], self.timeslots[m[1]], m[2])
                       for m in anns.values() if
                       self.timeslots[m[0]] <= time and
                       self.timeslots[m[1]] >= time])

Response: Give the annotations at the given time.


Instruction:
Implement a function in Python 3 to
give the annotation before a given time.

Response:
def get_annotation_data_after_time(self, id_tier, time):
        """Give the annotation before a given time. When the tier contains
        reference annotations this will be returned, check
        :func:`get_ref_annotation_data_before_time` for the format. If an
        annotation overlaps with ``time`` that annotation will be returned.

        :param str id_tier: Name of the tier.
        :param int time: Time to get the annotation before.
        :raises KeyError: If the tier is non existent.
        """
        if self.tiers[id_tier][1]:
            return self.get_ref_annotation_after_time(id_tier, time)
        befores = self.get_annotation_data_between_times(
            id_tier, time, self.get_full_time_interval()[1])
        if befores:
            return [min(befores, key=lambda x: x[0])]
        else:
            return []


Instruction:
Given the following Python 3 function, write the documentation
def get_annotation_data_before_time(self, id_tier, time):
        if self.tiers[id_tier][1]:
            return self.get_ref_annotation_before_time(id_tier, time)
        befores = self.get_annotation_data_between_times(id_tier, 0, time)
        if befores:
            return [max(befores, key=lambda x: x[0])]
        else:
            return []

Response: Give the annotation before a given time.


Instruction:
Can you create a Python 3 function that
returns the list of annotations within the times.

Response:
def get_annotation_data_between_times(self, id_tier, start, end):
        """Gives the annotations within the times.
        When the tier contains reference annotations this will be returned,
        check :func:`get_ref_annotation_data_between_times` for the format.

        :param str id_tier: Name of the tier.
        :param int start: Start time of the annotation.
        :param int end: End time of the annotation.
        :returns: List of annotations within that time.
        :raises KeyError: If the tier is non existent.
        """
        if self.tiers[id_tier][1]:
            return self.get_ref_annotation_data_between_times(
                id_tier, start, end)
        anns = ((self.timeslots[a[0]], self.timeslots[a[1]], a[2])
                for a in self.tiers[id_tier][0].values())
        return sorted(a for a in anns if a[1] >= start and a[0] <= end)


Instruction:
How would you explain what the following Python 3 function does
def get_annotation_data_for_tier(self, id_tier):
        if self.tiers[id_tier][1]:
            return self.get_ref_annotation_data_for_tier(id_tier)
        a = self.tiers[id_tier][0]
        return [(self.timeslots[a[b][0]], self.timeslots[a[b][1]], a[b][2])
                for b in a]

Response: Returns a list of annotations for the given tier.


Instruction:
Can you generate the documentation for the following Python 3 function
def get_child_tiers_for(self, id_tier):
        self.tiers[id_tier]
        return [m for m in self.tiers if 'PARENT_REF' in self.tiers[m][2] and
                self.tiers[m][2]['PARENT_REF'] == id_tier]

Response: Give all child tiers for a tier.


Instruction:
Can you create a Python 3 function that
gives the full time interval of the file.

Response:
def get_full_time_interval(self):
        """Give the full time interval of the file. Note that the real interval
        can be longer because the sound file attached can be longer.

        :returns: Tuple of the form: ``(min_time, max_time)``.
        """
        return (0, 0) if not self.timeslots else\
            (min(self.timeslots.values()), max(self.timeslots.values()))


Instruction:
Can you create a Python 3 function that
gives gaps and overlaps.

Response:
def get_gaps_and_overlaps(self, tier1, tier2, maxlen=-1):
        """Give gaps and overlaps. The return types are shown in the table
        below. The string will be of the format: ``id_tiername_tiername``.

        .. note:: There is also a faster method: :func:`get_gaps_and_overlaps2`

        For example when a gap occurs between tier1 and tier2 and they are
        called ``speakerA`` and ``speakerB`` the annotation value of that gap
        will be ``G12_speakerA_speakerB``.

        | The gaps and overlaps are calculated using Heldner and Edlunds
          method found in:
        | *Heldner, M., & Edlund, J. (2010). Pauses, gaps and overlaps in
          conversations. Journal of Phonetics, 38(4), 555–568.
          doi:10.1016/j.wocn.2010.08.002*

        +-----+---------------------------------------------+
        | id  | Description                                 |
        +=====+=============================================+
        | O12 | Overlap from tier1 to tier2                 |
        +-----+---------------------------------------------+
        | O21 | Overlap from tier2 to tier1                 |
        +-----+---------------------------------------------+
        | G12 | Between speaker gap from tier1 to tier2     |
        +-----+---------------------------------------------+
        | G21 | Between speaker gap from tier2 to tier1     |
        +-----+---------------------------------------------+
        | W12 | Within speaker overlap from tier2 in tier1  |
        +-----+---------------------------------------------+
        | W21 | Within speaker overlap from tier1 in tier2  |
        +-----+---------------------------------------------+
        | P1  | Pause for tier1                             |
        +-----+---------------------------------------------+
        | P2  | Pause for tier2                             |
        +-----+---------------------------------------------+

        :param str tier1: Name of the first tier.
        :param str tier2: Name of the second tier.
        :param int maxlen: Maximum length of gaps (skip longer ones), if ``-1``
                           no maximum will be used.
        :yields: Tuples of the form ``[(start, end, type)]``.
        :raises KeyError: If a tier is non existent.
        :raises IndexError: If no annotations are available in the tiers.
        """
        spkr1anns = sorted((self.timeslots[a[0]], self.timeslots[a[1]])
                           for a in self.tiers[tier1][0].values())
        spkr2anns = sorted((self.timeslots[a[0]], self.timeslots[a[1]])
                           for a in self.tiers[tier2][0].values())
        line1 = []

        def isin(x, lst):
            return False if\
                len([i for i in lst if i[0] <= x and i[1] >= x]) == 0 else True
        minmax = (min(spkr1anns[0][0], spkr2anns[0][0]),
                  max(spkr1anns[-1][1], spkr2anns[-1][1]))
        last = (1, minmax[0])
        for ts in range(*minmax):
            in1, in2 = isin(ts, spkr1anns), isin(ts, spkr2anns)
            if in1 and in2:      # Both speaking
                if last[0] == 'B':
                    continue
                ty = 'B'
            elif in1:            # Only 1 speaking
                if last[0] == '1':
                    continue
                ty = '1'
            elif in2:            # Only 2 speaking
                if last[0] == '2':
                    continue
                ty = '2'
            else:                # None speaking
                if last[0] == 'N':
                    continue
                ty = 'N'
            line1.append((last[0], last[1], ts))
            last = (ty, ts)
        line1.append((last[0], last[1], minmax[1]))
        for i in range(len(line1)):
            if line1[i][0] == 'N':
                if i != 0 and i < len(line1) - 1 and\
                        line1[i-1][0] != line1[i+1][0]:
                    t = ('G12', tier1, tier2) if line1[i-1][0] == '1' else\
                        ('G21', tier2, tier1)
                    if maxlen == -1 or abs(line1[i][1]-line1[i][2]) < maxlen:
                        yield (line1[i][1], line1[i][2]-1, '_'.join(t))
                else:
                    t = ('P1', tier1) if line1[i-1][0] == '1' else\
                        ('P2', tier2)
                    if maxlen == -1 or abs(line1[i][1]-line1[i][2]) < maxlen:
                        yield (line1[i][1], line1[i][2]-1, '_'.join(t))
            elif line1[i][0] == 'B':
                if i != 0 and i < len(line1) - 1 and\
                        line1[i-1][0] != line1[i+1][0]:
                    t = ('O12', tier1, tier2) if line1[i-1][0] == '1' else\
                        ('O21', tier2, tier1)
                    yield (line1[i][1], line1[i][2]-1, '_'.join(t))
                else:
                    t = ('W12', tier1, tier2) if line1[i-1][0] == '1' else\
                        ('W21', tier2, tier1)
                    yield (line1[i][1], line1[i][2]-1, '_'.join(t))


Instruction:
Here you have a function in Python 3, explain what it does
def get_gaps_and_overlaps2(self, tier1, tier2, maxlen=-1):
        ad = sorted(((a, i+1) for i, t in enumerate([tier1, tier2]) for a in
                     self.get_annotation_data_for_tier(t)), reverse=True)
        if ad:
            last = (lambda x: (x[0][0], x[0][1], x[1]))(ad.pop())

            def thr(x, y):
                return maxlen == -1 or abs(x-y) < maxlen
            while ad:
                (begin, end, _), current = ad.pop()
                if last[2] == current and thr(begin, last[1]):
                    yield (last[1], begin, 'P{}'.format(current))
                elif last[0] < begin and last[1] > end:
                    yield (begin, end, 'W{}{}'.format(last[2], current))
                    continue
                elif last[1] > begin:
                    yield (begin, last[1], 'O{}{}'.format(last[2], current))
                elif last[1] < begin and thr(begin, last[1]):
                    yield (last[1], begin, 'G{}{}'.format(last[2], current))
                last = (begin, end, current)

Response: A generator function that yields tuples of the first and last gaps and overlapped gaps.


Instruction:
Can you create a Python 3 function that
gives the ref annotations at the given time of the form [ begin end value refvalue ]

Response:
def get_ref_annotation_at_time(self, tier, time):
        """Give the ref annotations at the given time of the form
        ``[(start, end, value, refvalue)]``

        :param str tier: Name of the tier.
        :param int time: Time of the annotation of the parent.
        :returns: List of annotations at that time.
        :raises KeyError: If the tier is non existent.
        """
        bucket = []
        for aid, (ref, value, _, _) in self.tiers[tier][1].items():
            begin, end, rvalue, _ = self.tiers[self.annotations[ref]][0][ref]
            begin = self.timeslots[begin]
            end = self.timeslots[end]
            if begin <= time and end >= time:
                bucket.append((begin, end, value, rvalue))
        return bucket


Instruction:
Write a Python 3 script to
give the ref annotation after a time.

Response:
def get_ref_annotation_data_after_time(self, id_tier, time):
        """Give the ref annotation after a time. If an annotation overlaps
        with `ktime`` that annotation will be returned.

        :param str id_tier: Name of the tier.
        :param int time: Time to get the annotation after.
        :returns: Annotation after that time in a list
        :raises KeyError: If the tier is non existent.
        """
        befores = self.get_ref_annotation_data_between_times(
            id_tier, time, self.get_full_time_interval())
        if befores:
            return [min(befores, key=lambda x: x[0])]
        else:
            return []


Instruction:
Can you tell what is the following Python 3 function doing
def get_ref_annotation_data_before_time(self, id_tier, time):
        befores = self.get_ref_annotation_data_between_times(id_tier, 0, time)
        if befores:
            return [max(befores, key=lambda x: x[0])]
        else:
            return []

Response: Give the ref annotation before a time.


Instruction:
Write a Python 3 script for
giving the ref annotations between times of the form [ start end value refvalue )

Response:
def get_ref_annotation_data_between_times(self, id_tier, start, end):
        """Give the ref annotations between times of the form
        ``[(start, end, value, refvalue)]``

        :param str tier: Name of the tier.
        :param int start: End time of the annotation of the parent.
        :param int end: Start time of the annotation of the parent.
        :returns: List of annotations at that time.
        :raises KeyError: If the tier is non existent.
        """
        bucket = []
        for aid, (ref, value, _, _) in self.tiers[id_tier][1].items():
            begin, end, rvalue, _ = self.tiers[self.annotations[ref]][0][ref]
            begin = self.timeslots[begin]
            end = self.timeslots[end]
            if begin <= end and end >= begin:
                bucket.append((begin, end, value, rvalue))
        return bucket


Instruction:
How would you code a function in Python 3 to
give a list of all reference annotations of the form : start end value refvalue

Response:
def get_ref_annotation_data_for_tier(self, id_tier):
        """"Give a list of all reference annotations of the form:
        ``[(start, end, value, refvalue)]``

        :param str id_tier: Name of the tier.
        :raises KeyError: If the tier is non existent.
        :returns: Reference annotations within that tier.
        """
        bucket = []
        for aid, (ref, value, prev, _) in self.tiers[id_tier][1].items():
            refann = self.get_parent_aligned_annotation(ref)
            bucket.append((self.timeslots[refann[0]],
                           self.timeslots[refann[1]], value, refann[2]))
        return bucket


Instruction:
Write a Python 3 function for
giving the aligment annotation that a reference annotation belongs to directly or indirectly through other reference annotations.

Response:
def get_parent_aligned_annotation(self, ref_id):
        """" Give the aligment annotation that a reference annotation belongs to directly, or indirectly through other
        reference annotations.
        :param str ref_id: Id of a reference annotation.
        :raises KeyError: If no annotation exists with the id or if it belongs to an alignment annotation.
        :returns: The alignment annotation at the end of the reference chain.
        """
        parentTier = self.tiers[self.annotations[ref_id]]
        while "PARENT_REF" in parentTier[2] and len(parentTier[2]) > 0:
            ref_id = parentTier[1][ref_id][0]
            parentTier = self.tiers[self.annotations[ref_id]]

        return parentTier[0][ref_id]


Instruction:
Can you tell what is the following Python 3 function doing
def get_tier_ids_for_linguistic_type(self, ling_type, parent=None):
        return [t for t in self.tiers if
                self.tiers[t][2]['LINGUISTIC_TYPE_REF'] == ling_type and
                (parent is None or self.tiers[t][2]['PARENT_REF'] == parent)]

Response: Give a list of all tiers matching a linguistic type.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def insert_annotation(self, id_tier, start, end, value='', svg_ref=None):
        return self.add_annotation(id_tier, start, end, value, svg_ref)

Response: Insert an annotation into the cache.


Instruction:
Here you have a function in Python 3, explain what it does
def insert_ref_annotation(self, id_tier, tier2, time, value='',
                              prev=None, svg=None):
        """.. deprecated:: 1.2

        Use :func:`add_ref_annotation` instead.
        """
        return self.add_ref_annotation(id_tier, tier2, time, value, prev, svg)

Response: Insert a new ref annotation into the cache.


Instruction:
Make a summary of the following Python 3 code
def merge_tiers(self, tiers, tiernew=None, gapt=0, sep='_', safe=False):
        if tiernew is None:
            tiernew = u'{}_merged'.format('_'.join(tiers))
        self.add_tier(tiernew)
        aa = [(sys.maxsize, sys.maxsize, None)] + sorted((
            a for t in tiers for a in self.get_annotation_data_for_tier(t)),
            reverse=True)
        l = None
        while aa:
            begin, end, value = aa.pop()
            if l is None:
                l = [begin, end, [value]]
            elif begin - l[1] >= gapt:
                if not safe or l[1] > l[0]:
                    self.add_annotation(tiernew, l[0], l[1], sep.join(l[2]))
                l = [begin, end, [value]]
            else:
                if end > l[1]:
                    l[1] = end
                l[2].append(value)
        return tiernew

Response: Merge tiers into a new tier and optionally add annotations to the new tier.


Instruction:
Write a Python 3 script to
remove all annotations from a tier

Response:
def remove_all_annotations_from_tier(self, id_tier, clean=True):
        """remove all annotations from a tier

        :param str id_tier: Name of the tier.
        :raises KeyError: If the tier is non existent.
        """
        for aid in self.tiers[id_tier][0]:
            del(self.annotations[aid])
        for aid in self.tiers[id_tier][1]:
            del(self.annotations[aid])

        self.tiers[id_tier][0].clear()
        self.tiers[id_tier][1].clear()
        if clean:
            self.clean_time_slots()


Instruction:
Create a Python 3 function to
remove an annotation in a tier.

Response:
def remove_annotation(self, id_tier, time, clean=True):
        """Remove an annotation in a tier, if you need speed the best thing is
        to clean the timeslots after the last removal. When the tier contains
        reference annotations :func:`remove_ref_annotation` will be executed
        instead.

        :param str id_tier: Name of the tier.
        :param int time: Timepoint within the annotation.
        :param bool clean: Flag to clean the timeslots afterwards.
        :raises KeyError: If the tier is non existent.
        :returns: Number of removed annotations.
        """
        if self.tiers[id_tier][1]:
            return self.remove_ref_annotation(id_tier, time, clean)
        removed = 0
        for b in [a for a in self.tiers[id_tier][0].items() if
                  self.timeslots[a[1][0]] <= time and
                  self.timeslots[a[1][1]] >= time]:
            del(self.tiers[id_tier][0][b[0]])
            del(self.annotations[b[0]])
            removed += 1
        if clean:
            self.clean_time_slots()
        return removed


Instruction:
Can you generate a brief explanation for the following Python 3 code
def remove_cv_description(self, cv_id, lang_ref):
        for i, (l, d) in reversed(enumerate(
                self.controlled_vocabularies[cv_id][1])):
            if l == lang_ref:
                del(self.controlled_vocabularies[cv_id][1][i])

Response: Removes a description from a controlled vocabulary.


Instruction:
Write a Python 3 script to
remove all licenses matching both key and value.

Response:
def remove_license(self, name=None, url=None):
        """Remove all licenses matching both key and value.

        :param str name: Name of the license.
        :param str url: URL of the license.
        """
        for k, v in self.licenses[:]:
            if (name is None or name == k) and (url is None or url == v):
                del(self.licenses[self.licenses.index((k, v))])


Instruction:
How would you implement a function in Python 3 that
removes all linked files that match all the criteria.

Response:
def remove_linked_files(self, file_path=None, relpath=None, mimetype=None,
                            time_origin=None, ex_from=None):
        """Remove all linked files that match all the criteria, criterias that
        are ``None`` are ignored.

        :param str file_path: Path of the file.
        :param str relpath: Relative filepath.
        :param str mimetype: Mimetype of the file.
        :param int time_origin: Time origin.
        :param str ex_from: Extracted from.
        """
        for attrib in self.media_descriptors[:]:
            if file_path is not None and attrib['MEDIA_URL'] != file_path:
                continue
            if relpath is not None and attrib['RELATIVE_MEDIA_URL'] != relpath:
                continue
            if mimetype is not None and attrib['MIME_TYPE'] != mimetype:
                continue
            if time_origin is not None and\
                    attrib['TIME_ORIGIN'] != time_origin:
                continue
            if ex_from is not None and attrib['EXTRACTED_FROM'] != ex_from:
                continue
            del(self.media_descriptors[self.media_descriptors.index(attrib)])


Instruction:
Write a Python 3 script to
remove a property from the resource table.

Response:
def remove_property(self, key=None, value=None):
        """Remove all properties matching both key and value.

        :param str key: Key of the property.
        :param str value: Value of the property.
        """
        for k, v in self.properties[:]:
            if (key is None or key == k) and (value is None or value == v):
                del(self.properties[self.properties.index((k, v))])


Instruction:
How would you explain what the following Python 3 function does
def remove_ref_annotation(self, id_tier, time):
        removed = 0
        bucket = []
        for aid, (ref, value, _, _) in self.tiers[id_tier][1].items():
            begin, end, rvalue, _ = self.tiers[self.annotations[ref]][0][ref]
            begin = self.timeslots[begin]
            end = self.timeslots[end]
            if begin <= time and end >= time:
                removed += 1
                bucket.append(aid)
        for aid in bucket:
            del(self.tiers[id_tier][1][aid])
        return removed

Response: Removes a reference annotation.


Instruction:
Write a Python 3 script to
remove all secondary linked files that match all the criteria.

Response:
def remove_secondary_linked_files(self, file_path=None, relpath=None,
                                      mimetype=None, time_origin=None,
                                      assoc_with=None):
        """Remove all secondary linked files that match all the criteria,
        criterias that are ``None`` are ignored.

        :param str file_path: Path of the file.
        :param str relpath: Relative filepath.
        :param str mimetype: Mimetype of the file.
        :param int time_origin: Time origin.
        :param str ex_from: Extracted from.
        """
        for attrib in self.linked_file_descriptors[:]:
            if file_path is not None and attrib['LINK_URL'] != file_path:
                continue
            if relpath is not None and attrib['RELATIVE_LINK_URL'] != relpath:
                continue
            if mimetype is not None and attrib['MIME_TYPE'] != mimetype:
                continue
            if time_origin is not None and\
                    attrib['TIME_ORIGIN'] != time_origin:
                continue
            if assoc_with is not None and\
                    attrib['ASSOCIATED_WITH'] != assoc_with:
                continue
            del(self.linked_file_descriptors[
                self.linked_file_descriptors.index(attrib)])


Instruction:
How would you implement a function in Python 3 that
removes a tier from the cache.

Response:
def remove_tier(self, id_tier, clean=True):
        """Remove a tier.

        :param str id_tier: Name of the tier.
        :param bool clean: Flag to also clean the timeslots.
        :raises KeyError: If tier is non existent.
        """
        del(self.tiers[id_tier])
        if clean:
            self.clean_time_slots()


Instruction:
Can you implement a function in Python 3 that
removes multiple tiers from the cache.

Response:
def remove_tiers(self, tiers):
        """Remove multiple tiers, note that this is a lot faster then removing
        them individually because of the delayed cleaning of timeslots.

        :param list tiers: Names of the tier to remove.
        :raises KeyError: If a tier is non existent.
        """
        for a in tiers:
            self.remove_tier(a, clean=False)
        self.clean_time_slots()


Instruction:
How would you code a function in Python 3 to
rename a tier. Note that this renames also the child tiers that have the tier as a parent. :param str id_from: Original name of the tier. :param str id_to: Target name of the tier. :throws KeyError: If the tier doesnt' exist.

Response:
def rename_tier(self, id_from, id_to):
        """Rename a tier. Note that this renames also the child tiers that have
        the tier as a parent.

        :param str id_from: Original name of the tier.
        :param str id_to: Target name of the tier.
        :throws KeyError: If the tier doesnt' exist.
        """
        childs = self.get_child_tiers_for(id_from)
        self.tiers[id_to] = self.tiers.pop(id_from)
        self.tiers[id_to][2]['TIER_ID'] = id_to
        for child in childs:
            self.tiers[child][2]['PARENT_REF'] = id_to


Instruction:
Can you implement a function in Python 3 that
shifts all annotations in time.

Response:
def shift_annotations(self, time):
        """Shift all annotations in time. Annotations that are in the beginning
        and a left shift is applied can be squashed or discarded.

        :param int time: Time shift width, negative numbers make a left shift.
        :returns: Tuple of a list of squashed annotations and a list of removed
                  annotations in the format: ``(tiername, start, end, value)``.
        """
        total_re = []
        total_sq = []
        for name, tier in self.tiers.items():
            squashed = []
            for aid, (begin, end, value, _) in tier[0].items():
                if self.timeslots[end]+time <= 0:
                    squashed.append((name, aid))
                elif self.timeslots[begin]+time < 0:
                    total_sq.append((name, self.timeslots[begin],
                                     self.timeslots[end], value))
                    self.timeslots[begin] = 0
                else:
                    self.timeslots[begin] += time
                    self.timeslots[end] += time
            for name, aid in squashed:
                start, end, value, _ = self.tiers[name][0][aid]
                del(self.tiers[name][0][aid])
                del(self.annotations[aid])
                total_re.append(
                    (name, self.timeslots[start], self.timeslots[end], value))
        return total_sq, total_re


Instruction:
Here you have a function in Python 3, explain what it does
def to_textgrid(self, filtin=[], filtex=[], regex=False):
        from pympi.Praat import TextGrid
        _, end = self.get_full_time_interval()
        tgout = TextGrid(xmax=end/1000.0)
        func = (lambda x, y: re.match(x, y)) if regex else lambda x, y: x == y
        for tier in self.tiers:
            if (filtin and not any(func(f, tier) for f in filtin)) or\
                    (filtex and any(func(f, tier) for f in filtex)):
                continue
            ctier = tgout.add_tier(tier)
            for intv in self.get_annotation_data_for_tier(tier):
                try:
                    ctier.add_interval(intv[0]/1000.0, intv[1]/1000.0, intv[2])
                except:
                    pass
        return tgout

Response: Convert the object to a TextGrid object.


Instruction:
Here you have a function in Python 3, explain what it does
def main():

    import optparse
    import sys
    import codecs
    import locale
    import six
    from .algorithm import get_display

    parser = optparse.OptionParser()

    parser.add_option('-e', '--encoding',
                      dest='encoding',
                      default='utf-8',
                      type='string',
                      help='Text encoding (default: utf-8)')

    parser.add_option('-u', '--upper-is-rtl',
                      dest='upper_is_rtl',
                      default=False,
                      action='store_true',
                      help="Treat upper case chars as strong 'R' "
                      'for debugging (default: False).')

    parser.add_option('-d', '--debug',
                      dest='debug',
                      default=False,
                      action='store_true',
                      help="Output to stderr steps taken with the algorithm")

    parser.add_option('-b', '--base-dir',
                      dest='base_dir',
                      default=None,
                      type='string',
                      help="Override base direction [L|R]")

    options, rest = parser.parse_args()

    if options.base_dir and options.base_dir not in 'LR':
        parser.error('option -b can be L or R')

    # allow unicode in sys.stdout.write
    if six.PY2:
        sys.stdout = codecs.getwriter(locale.getpreferredencoding())(sys.stdout)

    if rest:
        lines = rest
    else:
        lines = sys.stdin

    for line in lines:
        display = get_display(line, options.encoding, options.upper_is_rtl,
                              options.base_dir, options.debug)
        # adjust the encoding as unicode, to match the output encoding
        if not isinstance(display, six.text_type):
            display = display.decode(options.encoding)

        six.print_(display, end='')

Response: This function will be used to create the console script


Instruction:
Can you create a Python 3 function that
displays debug information for the storage

Response:
def debug_storage(storage, base_info=False, chars=True, runs=False):
    "Display debug information for the storage"

    import codecs
    import locale
    import sys

    if six.PY2:
        stderr = codecs.getwriter(locale.getpreferredencoding())(sys.stderr)
    else:
        stderr = sys.stderr

    caller = inspect.stack()[1][3]
    stderr.write('in %s\n' % caller)

    if base_info:
        stderr.write(u'  base level  : %d\n' % storage['base_level'])
        stderr.write(u'  base dir    : %s\n' % storage['base_dir'])

    if runs:
        stderr.write(u'  runs        : %s\n' % list(storage['runs']))

    if chars:
        output = u'  Chars       : '
        for _ch in storage['chars']:
            if _ch != '\n':
                output += _ch['ch']
            else:
                output += 'C'
        stderr.write(output + u'\n')

        output = u'  Res. levels : %s\n' % u''.join(
            [six.text_type(_ch['level']) for _ch in storage['chars']])
        stderr.write(output)

        _types = [_ch['type'].ljust(3) for _ch in storage['chars']]

        for i in range(3):
            if i:
                output = u'                %s\n'
            else:
                output = u'  Res. types  : %s\n'
            stderr.write(output % u''.join([_t[i] for _t in _types]))


Instruction:
Explain what the following Python 3 code does
def get_base_level(text, upper_is_rtl=False):

    base_level = None

    prev_surrogate = False
    # P2
    for _ch in text:
        # surrogate in case of ucs2
        if _IS_UCS2 and (_SURROGATE_MIN <= ord(_ch) <= _SURROGATE_MAX):
            prev_surrogate = _ch
            continue
        elif prev_surrogate:
            _ch = prev_surrogate + _ch
            prev_surrogate = False

        # treat upper as RTL ?
        if upper_is_rtl and _ch.isupper():
            base_level = 1
            break

        bidi_type = bidirectional(_ch)

        if bidi_type in ('AL', 'R'):
            base_level = 1
            break

        elif bidi_type == 'L':
            base_level = 0
            break

    # P3
    if base_level is None:
        base_level = 0

    return base_level

Response: Get the base embedding level of a text. Returns 0 for LTR 1 for RTL.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def get_embedding_levels(text, storage, upper_is_rtl=False, debug=False):

    prev_surrogate = False
    base_level = storage['base_level']

    # preset the storage's chars
    for _ch in text:
        if _IS_UCS2 and (_SURROGATE_MIN <= ord(_ch) <= _SURROGATE_MAX):
            prev_surrogate = _ch
            continue
        elif prev_surrogate:
            _ch = prev_surrogate + _ch
            prev_surrogate = False

        if upper_is_rtl and _ch.isupper():
            bidi_type = 'R'
        else:
            bidi_type = bidirectional(_ch)

        storage['chars'].append({
            'ch': _ch,
            'level': base_level,
            'type': bidi_type,
            'orig': bidi_type
        })
    if debug:
        debug_storage(storage, base_info=True)

Response: Get the base embedding level and direction of the text.


Instruction:
Here you have a function in Python 3, explain what it does
def explicit_embed_and_overrides(storage, debug=False):
    overflow_counter = almost_overflow_counter = 0
    directional_override = 'N'
    levels = deque()

    # X1
    embedding_level = storage['base_level']

    for _ch in storage['chars']:
        bidi_type = _ch['type']

        level_func, override = X2_X5_MAPPINGS.get(bidi_type, (None, None))

        if level_func:
            # So this is X2 to X5
            # if we've past EXPLICIT_LEVEL_LIMIT, note it and do nothing

            if overflow_counter != 0:
                overflow_counter += 1
                continue

            new_level = level_func(embedding_level)
            if new_level < EXPLICIT_LEVEL_LIMIT:
                levels.append((embedding_level, directional_override))
                embedding_level, directional_override = new_level, override

            elif embedding_level == EXPLICIT_LEVEL_LIMIT - 2:
                # The new level is invalid, but a valid level can still be
                # achieved if this level is 60 and we encounter an RLE or
                # RLO further on.  So record that we 'almost' overflowed.
                almost_overflow_counter += 1

            else:
                overflow_counter += 1
        else:
            # X6
            if bidi_type not in X6_IGNORED:
                _ch['level'] = embedding_level
                if directional_override != 'N':
                    _ch['type'] = directional_override

            # X7
            elif bidi_type == 'PDF':
                if overflow_counter:
                    overflow_counter -= 1
                elif almost_overflow_counter and \
                        embedding_level != EXPLICIT_LEVEL_LIMIT - 1:
                    almost_overflow_counter -= 1
                elif levels:
                    embedding_level, directional_override = levels.pop()

            # X8
            elif bidi_type == 'B':
                levels.clear()
                overflow_counter = almost_overflow_counter = 0
                embedding_level = _ch['level'] = storage['base_level']
                directional_override = 'N'

    # Removes the explicit embeds and overrides of types
    # RLE, LRE, RLO, LRO, PDF, and BN. Adjusts extended chars
    # next and prev as well

    # Applies X9. See http://unicode.org/reports/tr9/#X9
    storage['chars'] = [_ch for _ch in storage['chars']
                        if _ch['type'] not in X9_REMOVED]

    calc_level_runs(storage)

    if debug:
        debug_storage(storage, runs=True)

Response: Apply X1 to X9 rules of the unicode algorithm.


Instruction:
Make a summary of the following Python 3 code
def calc_level_runs(storage):
    # run level depends on the higher of the two levels on either side of
    # the boundary If the higher level is odd, the type is R; otherwise,
    # it is L

    storage['runs'].clear()
    chars = storage['chars']

    # empty string ?
    if not chars:
        return

    def calc_level_run(b_l, b_r):
        return ['L', 'R'][max(b_l, b_r) % 2]

    first_char = chars[0]

    sor = calc_level_run(storage['base_level'], first_char['level'])
    eor = None

    run_start = run_length = 0

    prev_level, prev_type = first_char['level'], first_char['type']

    for _ch in chars:
        curr_level, curr_type = _ch['level'], _ch['type']

        if curr_level == prev_level:
            run_length += 1
        else:
            eor = calc_level_run(prev_level, curr_level)
            storage['runs'].append({'sor': sor, 'eor': eor, 'start': run_start,
                                    'type': prev_type, 'length': run_length})
            sor = eor
            run_start += run_length
            run_length = 1

        prev_level, prev_type = curr_level, curr_type

    # for the last char/runlevel
    eor = calc_level_run(curr_level, storage['base_level'])
    storage['runs'].append({'sor': sor, 'eor': eor, 'start': run_start,
                            'type': curr_type, 'length': run_length})

Response: Split the storage to run of char types at the same level.


Instruction:
How would you code a function in Python 3 to
resolve weak types of the European numbers and ALs in the given storage.

Response:
def resolve_weak_types(storage, debug=False):
    """Reslove weak type rules W1 - W3.

    See: http://unicode.org/reports/tr9/#Resolving_Weak_Types

    """

    for run in storage['runs']:
        prev_strong = prev_type = run['sor']
        start, length = run['start'], run['length']
        chars = storage['chars'][start:start+length]
        for _ch in chars:
            # W1. Examine each nonspacing mark (NSM) in the level run, and
            # change the type of the NSM to the type of the previous character.
            # If the NSM is at the start of the level run, it will get the type
            # of sor.
            bidi_type = _ch['type']

            if bidi_type == 'NSM':
                _ch['type'] = bidi_type = prev_type

            # W2. Search backward from each instance of a European number until
            # the first strong type (R, L, AL, or sor) is found. If an AL is
            # found, change the type of the European number to Arabic number.
            if bidi_type == 'EN' and prev_strong == 'AL':
                _ch['type'] = 'AN'

            # update prev_strong if needed
            if bidi_type in ('R', 'L', 'AL'):
                prev_strong = bidi_type

            prev_type = _ch['type']

        # W3. Change all ALs to R
        for _ch in chars:
            if _ch['type'] == 'AL':
                _ch['type'] = 'R'

        # W4. A single European separator between two European numbers changes
        # to a European number. A single common separator between two numbers of
        # the same type changes to that type.
        for idx in range(1, len(chars) - 1):
            bidi_type = chars[idx]['type']
            prev_type = chars[idx-1]['type']
            next_type = chars[idx+1]['type']

            if bidi_type == 'ES' and (prev_type == next_type == 'EN'):
                chars[idx]['type'] = 'EN'

            if bidi_type == 'CS' and prev_type == next_type and \
                    prev_type in ('AN', 'EN'):
                chars[idx]['type'] = prev_type

        # W5. A sequence of European terminators adjacent to European numbers
        # changes to all European numbers.
        for idx in range(len(chars)):
            if chars[idx]['type'] == 'EN':
                for et_idx in range(idx-1, -1, -1):
                    if chars[et_idx]['type'] == 'ET':
                        chars[et_idx]['type'] = 'EN'
                    else:
                        break
                for et_idx in range(idx+1, len(chars)):
                    if chars[et_idx]['type'] == 'ET':
                        chars[et_idx]['type'] = 'EN'
                    else:
                        break

        # W6. Otherwise, separators and terminators change to Other Neutral.
        for _ch in chars:
            if _ch['type'] in ('ET', 'ES', 'CS'):
                _ch['type'] = 'ON'

        # W7. Search backward from each instance of a European number until the
        # first strong type (R, L, or sor) is found. If an L is found, then
        # change the type of the European number to L.
        prev_strong = run['sor']
        for _ch in chars:
            if _ch['type'] == 'EN' and prev_strong == 'L':
                _ch['type'] = 'L'

            if _ch['type'] in ('L', 'R'):
                prev_strong = _ch['type']

    if debug:
        debug_storage(storage, runs=True)


Instruction:
How would you code a function in Python 3 to
resolve the neutral types in the given storage.

Response:
def resolve_neutral_types(storage, debug):
    """Resolving neutral types. Implements N1 and N2

    See: http://unicode.org/reports/tr9/#Resolving_Neutral_Types

    """

    for run in storage['runs']:
        start, length = run['start'], run['length']
        # use sor and eor
        chars = [{'type': run['sor']}] + storage['chars'][start:start+length] +\
                [{'type': run['eor']}]
        total_chars = len(chars)

        seq_start = None
        for idx in range(total_chars):
            _ch = chars[idx]
            if _ch['type'] in ('B', 'S', 'WS', 'ON'):
                # N1. A sequence of neutrals takes the direction of the
                # surrounding strong text if the text on both sides has the same
                # direction. European and Arabic numbers act as if they were R
                # in terms of their influence on neutrals. Start-of-level-run
                # (sor) and end-of-level-run (eor) are used at level run
                # boundaries.
                if seq_start is None:
                    seq_start = idx
                    prev_bidi_type = chars[idx-1]['type']
            else:
                if seq_start is not None:
                    next_bidi_type = chars[idx]['type']

                    if prev_bidi_type in ('AN', 'EN'):
                        prev_bidi_type = 'R'

                    if next_bidi_type in ('AN', 'EN'):
                        next_bidi_type = 'R'

                    for seq_idx in range(seq_start, idx):
                        if prev_bidi_type == next_bidi_type:
                            chars[seq_idx]['type'] = prev_bidi_type
                        else:
                            # N2. Any remaining neutrals take the embedding
                            # direction. The embedding direction for the given
                            # neutral character is derived from its embedding
                            # level: L if the character is set to an even level,
                            # and R if the level is odd.
                            chars[seq_idx]['type'] = \
                                _embedding_direction(chars[seq_idx]['level'])

                    seq_start = None

    if debug:
        debug_storage(storage)


Instruction:
Explain what the following Python 3 code does
def resolve_implicit_levels(storage, debug):
    for run in storage['runs']:
        start, length = run['start'], run['length']
        chars = storage['chars'][start:start+length]

        for _ch in chars:
            # only those types are allowed at this stage
            assert _ch['type'] in ('L', 'R', 'EN', 'AN'),\
                    '%s not allowed here' % _ch['type']

            if _embedding_direction(_ch['level']) == 'L':
                # I1. For all characters with an even (left-to-right) embedding
                # direction, those of type R go up one level and those of type
                # AN or EN go up two levels.
                if _ch['type'] == 'R':
                    _ch['level'] += 1
                elif _ch['type'] != 'L':
                    _ch['level'] += 2
            else:
                # I2. For all characters with an odd (right-to-left) embedding
                # direction, those of type L, EN or AN  go up one level.
                if _ch['type'] != 'R':
                    _ch['level'] += 1

    if debug:
        debug_storage(storage, runs=True)

Response: Resolves implicit levels in the given storage.


Instruction:
Given the following Python 3 function, write the documentation
def reverse_contiguous_sequence(chars, line_start, line_end, highest_level,
                                lowest_odd_level):
    """L2. From the highest level found in the text to the lowest odd
    level on each line, including intermediate levels not actually
    present in the text, reverse any contiguous sequence of characters
    that are at that level or higher.

    """
    for level in range(highest_level, lowest_odd_level-1, -1):
        _start = _end = None

        for run_idx in range(line_start, line_end+1):
            run_ch = chars[run_idx]

            if run_ch['level'] >= level:
                if _start is None:
                    _start = _end = run_idx
                else:
                    _end = run_idx
            else:
                if _end:
                    chars[_start:+_end+1] = \
                            reversed(chars[_start:+_end+1])
                    _start = _end = None

        # anything remaining ?
        if _start is not None:
            chars[_start:+_end+1] = \
                reversed(chars[_start:+_end+1])

Response:
L2. From the highest level found in the text to the lowest odd
    level on each line and including intermediate levels not actually necessary.


Instruction:
Can you tell what is the following Python 3 function doing
def reorder_resolved_levels(storage, debug):

    # Applies L1.

    should_reset = True
    chars = storage['chars']

    for _ch in chars[::-1]:
        # L1. On each line, reset the embedding level of the following
        # characters to the paragraph embedding level:
        if _ch['orig'] in ('B', 'S'):
            # 1. Segment separators,
            # 2. Paragraph separators,
            _ch['level'] = storage['base_level']
            should_reset = True
        elif should_reset and _ch['orig'] in ('BN', 'WS'):
            # 3. Any sequence of whitespace characters preceding a segment
            # separator or paragraph separator
            # 4. Any sequence of white space characters at the end of the
            # line.
            _ch['level'] = storage['base_level']
        else:
            should_reset = False

    max_len = len(chars)

    # L2 should be per line
    # Calculates highest level and loweset odd level on the fly.

    line_start = line_end = 0
    highest_level = 0
    lowest_odd_level = EXPLICIT_LEVEL_LIMIT

    for idx in range(max_len):
        _ch = chars[idx]

        # calc the levels
        char_level = _ch['level']
        if char_level > highest_level:
            highest_level = char_level

        if char_level % 2 and char_level < lowest_odd_level:
            lowest_odd_level = char_level

        if _ch['orig'] == 'B' or idx == max_len - 1:
            line_end = idx
            # omit line breaks
            if _ch['orig'] == 'B':
                line_end -= 1

            reverse_contiguous_sequence(chars, line_start, line_end,
                                        highest_level, lowest_odd_level)

            # reset for next line run
            line_start = idx+1
            highest_level = 0
            lowest_odd_level = EXPLICIT_LEVEL_LIMIT

    if debug:
        debug_storage(storage)

Response: Reorder the resolved levels of the language classes.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def apply_mirroring(storage, debug):
    # L4. A character is depicted by a mirrored glyph if and only if (a) the
    # resolved directionality of that character is R, and (b) the
    # Bidi_Mirrored property value of that character is true.
    for _ch in storage['chars']:
        unichar = _ch['ch']
        if mirrored(unichar) and \
                _embedding_direction(_ch['level']) == 'R':
            _ch['ch'] = MIRRORED.get(unichar, unichar)

    if debug:
        debug_storage(storage)

Response: Applies mirroring to the base character list.


Instruction:
Make a summary of the following Python 3 code
def get_display(unicode_or_str, encoding='utf-8', upper_is_rtl=False,
                base_dir=None, debug=False):
    """Accepts unicode or string. In case it's a string, `encoding`
    is needed as it works on unicode ones (default:"utf-8").

    Set `upper_is_rtl` to True to treat upper case chars as strong 'R'
    for debugging (default: False).

    Set `base_dir` to 'L' or 'R' to override the calculated base_level.

    Set `debug` to True to display (using sys.stderr) the steps taken with the
    algorithm.

    Returns the display layout, either as unicode or `encoding` encoded
    string.

    """
    storage = get_empty_storage()

    # utf-8 ? we need unicode
    if isinstance(unicode_or_str, six.text_type):
        text = unicode_or_str
        decoded = False
    else:
        text = unicode_or_str.decode(encoding)
        decoded = True

    if base_dir is None:
        base_level = get_base_level(text, upper_is_rtl)
    else:
        base_level = PARAGRAPH_LEVELS[base_dir]

    storage['base_level'] = base_level
    storage['base_dir'] = ('L', 'R')[base_level]

    get_embedding_levels(text, storage, upper_is_rtl, debug)
    explicit_embed_and_overrides(storage, debug)
    resolve_weak_types(storage, debug)
    resolve_neutral_types(storage, debug)
    resolve_implicit_levels(storage, debug)
    reorder_resolved_levels(storage, debug)
    apply_mirroring(storage, debug)

    chars = storage['chars']
    display = u''.join([_ch['ch'] for _ch in chars])

    if decoded:
        return display.encode(encoding)
    else:
        return display

Response: Returns the display layout of the given unicode or string.


Instruction:
How would you explain what the following Python 3 function does
def process(self, context):
        import os
        from maya import cmds

        """Inject the current working file"""
        current_file = cmds.file(sceneName=True, query=True)

        # Maya returns forward-slashes by default
        normalised = os.path.normpath(current_file)

        context.set_data('currentFile', value=normalised)

        # For backwards compatibility
        context.set_data('current_file', value=normalised)

Response: Inject the current working file


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

    def parse(line):
        line = line.replace("from PySide2 import", "from Qt import")
        line = line.replace("QtWidgets.QApplication.translate",
                            "Qt.QtCompat.translate")
        return line

    parsed = list()
    for line in lines:
        line = parse(line)
        parsed.append(line)

    return parsed

Response: Convert compiled. ui file from PySide2 to Qt. py


Instruction:
Write a Python 3 function for
adding an attribute to the object.

Response:
def _add(object, name, value):
    """Append to self, accessible via Qt.QtCompat"""
    self.__added__.append(name)
    setattr(object, name, value)


Instruction:
How would you explain what the following Python 3 function does
def cli(args):
    import argparse

    parser = argparse.ArgumentParser()
    parser.add_argument("--convert",
                        help="Path to compiled Python module, e.g. my_ui.py")
    parser.add_argument("--compile",
                        help="Accept raw .ui file and compile with native "
                             "PySide2 compiler.")
    parser.add_argument("--stdout",
                        help="Write to stdout instead of file",
                        action="store_true")
    parser.add_argument("--stdin",
                        help="Read from stdin instead of file",
                        action="store_true")

    args = parser.parse_args(args)

    if args.stdout:
        raise NotImplementedError("--stdout")

    if args.stdin:
        raise NotImplementedError("--stdin")

    if args.compile:
        raise NotImplementedError("--compile")

    if args.convert:
        sys.stdout.write("#\n"
                         "# WARNING: --convert is an ALPHA feature.\n#\n"
                         "# See https://github.com/mottosso/Qt.py/pull/132\n"
                         "# for details.\n"
                         "#\n")

        #
        # ------> Read
        #
        with open(args.convert) as f:
            lines = convert(f.readlines())

        backup = "%s_backup%s" % os.path.splitext(args.convert)
        sys.stdout.write("Creating \"%s\"..\n" % backup)
        shutil.copy(args.convert, backup)

        #
        # <------ Write
        #
        with open(args.convert, "w") as f:
            f.write("".join(lines))

        sys.stdout.write("Successfully converted \"%s\"\n" % args.convert)

Response: Qt command - line interface for the Sequence System.


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

    for member in ("__binding__",
                   "__binding_version__",
                   "__qt_version__",
                   "__added__",
                   "__remapped__",
                   "__modified__",
                   "convert",
                   "load_ui",
                   "translate"):
        setattr(binding, member, getattr(self, member))
        self.__added__.append(member)

    setattr(binding, "__wrapper_version__", self.__version__)
    self.__added__.append("__wrapper_version__")

Response: Maintain backwards compatibility for the current version of the current version of the current version of the current version of the current version.


Instruction:
Implement a function in Python 3 to
try showing the most desirable GUI

Response:
def show():
    """Try showing the most desirable GUI

    This function cycles through the currently registered
    graphical user interfaces, if any, and presents it to
    the user.

    """

    parent = next(
        o for o in QtWidgets.QApplication.instance().topLevelWidgets()
        if o.objectName() == "MayaWindow"
    )

    gui = _discover_gui()

    if gui is None:
        _show_no_gui()
    else:
        return gui(parent)


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

    # Prefer last registered
    guis = reversed(pyblish.api.registered_guis())

    for gui in guis:
        try:
            gui = __import__(gui).show
        except (ImportError, AttributeError):
            continue
        else:
            return gui

Response: Return the most desirable GUI of the currently registered GUIs


Instruction:
Here you have a function in Python 3, explain what it does
def teardown():
    if not self._has_been_setup:
        return

    deregister_plugins()
    deregister_host()

    if self._has_menu:
        remove_from_filemenu()
        self._has_menu = False

    self._has_been_setup = False
    print("pyblish: Integration torn down successfully")

Response: Remove integration from file menu and plugins


Instruction:
How would you implement a function in Python 3 that
adds Pyblish to file - menu.

Response:
def add_to_filemenu():
    """Add Pyblish to file-menu

    .. note:: We're going a bit hacky here, probably due to my lack
        of understanding for `evalDeferred` or `executeDeferred`,
        so if you can think of a better solution, feel free to edit.

    """

    if hasattr(cmds, 'about') and not cmds.about(batch=True):
        # As Maya builds its menus dynamically upon being accessed,
        # we force its build here prior to adding our entry using it's
        # native mel function call.
        mel.eval("evalDeferred buildFileMenu")

        # Serialise function into string
        script = inspect.getsource(_add_to_filemenu)
        script += "\n_add_to_filemenu()"

        # If cmds doesn't have any members, we're most likely in an
        # uninitialized batch-mode. It it does exists, ensure we
        # really aren't in batch mode.
        cmds.evalDeferred(script)


Instruction:
Explain what the following Python 3 code does
def maintained_selection():

    previous_selection = cmds.ls(selection=True)
    try:
        yield
    finally:
        if previous_selection:
            cmds.select(previous_selection,
                        replace=True,
                        noExpand=True)
        else:
            cmds.select(deselect=True,
                        noExpand=True)

Response: Maintain selection during context


Instruction:
Create a Python 3 function to
maintain current time during context

Response:
def maintained_time():
    """Maintain current time during context

    Example:
        >>> with maintained_time():
        ...    cmds.playblast()
        >>> # Time restored

    """

    ct = cmds.currentTime(query=True)
    try:
        yield
    finally:
        cmds.currentTime(ct, edit=True)


Instruction:
Write a Python 3 function that can
show a popup with no GUI.

Response:
def _show_no_gui():
    """Popup with information about how to register a new GUI

    In the event of no GUI being registered or available,
    this information dialog will appear to guide the user
    through how to get set up with one.

    """

    messagebox = QtWidgets.QMessageBox()
    messagebox.setIcon(messagebox.Warning)
    messagebox.setWindowIcon(QtGui.QIcon(os.path.join(
        os.path.dirname(pyblish.__file__),
        "icons",
        "logo-32x32.svg"))
    )

    spacer = QtWidgets.QWidget()
    spacer.setMinimumSize(400, 0)
    spacer.setSizePolicy(QtWidgets.QSizePolicy.Minimum,
                         QtWidgets.QSizePolicy.Expanding)

    layout = messagebox.layout()
    layout.addWidget(spacer, layout.rowCount(), 0, 1, layout.columnCount())

    messagebox.setWindowTitle("Uh oh")

    text = "No registered GUI found.\n\n"

    if not pyblish.api.registered_guis():
        text += (
            "In order to show you a GUI, one must first be registered. "
            "\n"
            "Pyblish supports one or more graphical user interfaces "
            "to be registered at once, the next acting as a fallback to "
            "the previous."
            "\n"
            "\n"
            "For example, to use Pyblish Lite, first install it:"
            "\n"
            "\n"
            "$ pip install pyblish-lite"
            "\n"
            "\n"
            "Then register it, like so:"
            "\n"
            "\n"
            ">>> import pyblish.api\n"
            ">>> pyblish.api.register_gui(\"pyblish_lite\")"
            "\n"
            "\n"
            "The next time you try running this, Lite will appear."
            "\n"
            "See http://api.pyblish.com/register_gui.html for "
            "more information."
        )
    else:
        text += (
            "None of the registered graphical user interfaces "
            "could be found."
            "\n"
            "These interfaces are currently registered:"
            "\n"
            "%s" % "\n".join(pyblish.api.registered_guis())
        )

    messagebox.setText(text)
    messagebox.setStandardButtons(messagebox.Ok)
    messagebox.exec_()


Instruction:
Explain what the following Python 3 code does
def get_cumulative_data(self):
		sets = map(itemgetter('data'), self.data)
		if not sets:
			return
		sum = sets.pop(0)
		yield sum
		while sets:
			sum = map(add, sets.pop(0))
			yield sum

Response: Get the data as it will be charted.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def get_single_axis_values(self, axis, dataset):
		data_index = getattr(self, '%s_data_index' % axis)
		return [p[data_index] for p in dataset['data']]

Response: Get the values for a single axis of the data.


Instruction:
Here you have a function in Python 3, explain what it does
def __draw_constant_line(self, value_label_style):
		"Draw a constant line on the y-axis with the label"
		value, label, style = value_label_style
		start = self.transform_output_coordinates((0, value))[1]
		stop = self.graph_width
		path = etree.SubElement(self.graph, 'path', {
			'd': 'M 0 %(start)s h%(stop)s' % locals(),
			'class': 'constantLine'})
		if style:
			path.set('style', style)
		text = etree.SubElement(self.graph, 'text', {
			'x': str(2),
			'y': str(start - 2),
			'class': 'constantLine'})
		text.text = label

Response: Draw a constant line on the y - axis with the label


Instruction:
Write a Python 3 script to
cache the parameters necessary to transform x & y coordinates

Response:
def load_transform_parameters(self):
		"Cache the parameters necessary to transform x & y coordinates"
		x_min, x_max, x_div = self.x_range()
		y_min, y_max, y_div = self.y_range()
		x_step = (float(self.graph_width) - self.font_size * 2) / \
			(x_max - x_min)
		y_step = (float(self.graph_height) - self.font_size * 2) / \
			(y_max - y_min)
		self.__transform_parameters = dict(locals())
		del self.__transform_parameters['self']


Instruction:
Here you have a function in Python 3, explain what it does
def reverse_mapping(mapping):
	keys, values = zip(*mapping.items())
	return dict(zip(values, keys))

Response:
Returns the mapping for every key value pair return the mapping for the
	equivalent value key pair


Instruction:
Explain what the following Python 3 code does
def flatten_mapping(mapping):
	return {
		key: value
		for keys, value in mapping.items()
		for key in always_iterable(keys)
	}

Response:
For every key that has an __iter__ method, assign the values
	to a key for each.

	>>> flatten_mapping({'ab': 3, ('c','d'): 4}) == {'ab': 3, 'c': 4, 'd': 4}
	True


Instruction:
Here you have a function in Python 3, explain what it does
def float_range(start=0, stop=None, step=1):
	start = float(start)
	while start < stop:
		yield start
		start += step

Response: A generator that yields floats from start to stop.


Instruction:
Given the following Python 3 function, write the documentation
def add_data(self, data_descriptor):
		pairs = itertools.zip_longest(self.data, data_descriptor['data'])
		self.data = list(itertools.starmap(robust_add, pairs))

Response: Adds a data set to the graph.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def add_data(self, conf):
		self.validate_data(conf)
		self.process_data(conf)
		self.data.append(conf)

Response: Add data to the graph object.


Instruction:
Create a Python 3 function for
processing the template with the data and config which has been set and return the resulting SVG. Raises ValueError when no data set has been added to the graph object.

Response:
def burn(self):
		"""
		Process the template with the data and
		config which has been set and return the resulting SVG.

		Raises ValueError when no data set has
		been added to the graph object.
		"""
		if not self.data:
			raise ValueError("No data available")

		if hasattr(self, 'calculations'):
			self.calculations()

		self.start_svg()
		self.calculate_graph_dimensions()
		self.foreground = etree.Element("g")
		self.draw_graph()
		self.draw_titles()
		self.draw_legend()
		self.draw_data()
		self.graph.append(self.foreground)
		self.render_inline_styles()

		return self.render(self.root)


Instruction:
Implement a Python 3 function for
calculating the margin to the left of the plot area setting border_left.

Response:
def calculate_left_margin(self):
		"""
		Calculates the margin to the left of the plot area, setting
		border_left.
		"""
		bl = 7
		# Check for Y labels
		if self.rotate_y_labels:
			max_y_label_height_px = self.y_label_font_size
		else:
			label_lengths = map(len, self.get_y_labels())
			max_y_label_len = max(label_lengths)
			max_y_label_height_px = 0.6 * max_y_label_len * self.y_label_font_size
		if self.show_y_labels:
			bl += max_y_label_height_px
		if self.stagger_y_labels:
			bl += max_y_label_height_px + 10
		if self.show_y_title:
			bl += self.y_title_font_size + 5
		self.border_left = bl


Instruction:
Can you implement a function in Python 3 that
calculates the margin in pixels to the right of the plot area.

Response:
def calculate_right_margin(self):
		"""
		Calculate the margin in pixels to the right of the plot area,
		setting border_right.
		"""
		br = 7
		if self.key and self.key_position == 'right':
			max_key_len = max(map(len, self.keys()))
			br += max_key_len * self.key_font_size * 0.6
			br += self.KEY_BOX_SIZE
			br += 10		# Some padding around the box
		self.border_right = br


Instruction:
Given the following Python 3 function, write the documentation
def calculate_top_margin(self):
		self.border_top = 5
		if self.show_graph_title:
			self.border_top += self.title_font_size
		self.border_top += 5
		if self.show_graph_subtitle:
			self.border_top += self.subtitle_font_size

Response:
Calculate the margin in pixels above the plot area setting
	border_top.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def add_popup(self, x, y, label):
		txt_width = len(label) * self.font_size * 0.6 + 10
		tx = x + [5, -5][int(x + txt_width > self.width)]
		anchor = ['start', 'end'][x + txt_width > self.width]
		style = 'fill: #000; text-anchor: %s;' % anchor
		id = 'label-%s' % self._w3c_name(label)
		attrs = {
			'x': str(tx),
			'y': str(y - self.font_size),
			'visibility': 'hidden',
			'style': style,
			'text': label,
			'id': id,
		}
		etree.SubElement(self.foreground, 'text', attrs)

		# add the circle element to the foreground
		vis_tmpl = (
			"document.getElementById('{id}').setAttribute('visibility', {val})"
		)
		attrs = {
			'cx': str(x),
			'cy': str(y),
			'r': str(10),
			'style': 'opacity: 0;',
			'onmouseover': vis_tmpl.format(val='visible', id=id),
			'onmouseout': vis_tmpl.format(val='hidden', id=id),
		}
		etree.SubElement(self.foreground, 'circle', attrs)

Response: Add pop - up information to a point on the graph.


Instruction:
Explain what the following Python 3 code does
def calculate_bottom_margin(self):
		bb = 7
		if self.key and self.key_position == 'bottom':
			bb += len(self.data) * (self.font_size + 5)
			bb += 10
		if self.show_x_labels:
			max_x_label_height_px = self.x_label_font_size
			if self.rotate_x_labels:
				label_lengths = map(len, self.get_x_labels())
				max_x_label_len = functools.reduce(max, label_lengths)
				max_x_label_height_px *= 0.6 * max_x_label_len
			bb += max_x_label_height_px
			if self.stagger_x_labels:
				bb += max_x_label_height_px + 10
		if self.show_x_title:
			bb += self.x_title_font_size + 5
		self.border_bottom = bb

Response:
Calculate the bottom margin in pixels below the plot area setting
	border_bottom.


Instruction:
Explain what the following Python 3 code does
def draw_graph(self):
		transform = 'translate (%s %s)' % (self.border_left, self.border_top)
		self.graph = etree.SubElement(self.root, 'g', transform=transform)

		etree.SubElement(self.graph, 'rect', {
			'x': '0',
			'y': '0',
			'width': str(self.graph_width),
			'height': str(self.graph_height),
			'class': 'graphBackground'
		})

		# Axis
		etree.SubElement(self.graph, 'path', {
			'd': 'M 0 0 v%s' % self.graph_height,
			'class': 'axis',
			'id': 'xAxis'
		})
		etree.SubElement(self.graph, 'path', {
			'd': 'M 0 %s h%s' % (self.graph_height, self.graph_width),
			'class': 'axis',
			'id': 'yAxis'
		})

		self.draw_x_labels()
		self.draw_y_labels()

Response: This method draws the graph.


Instruction:
Make a summary of the following Python 3 code
def make_datapoint_text(self, x, y, value, style=None):
		if not self.show_data_values:
			# do nothing
			return
		# first lay down the text in a wide white stroke to
		#  differentiate it from the background
		e = etree.SubElement(self.foreground, 'text', {
			'x': str(x),
			'y': str(y),
			'class': 'dataPointLabel',
			'style': '%(style)s stroke: #fff; stroke-width: 2;' % vars(),
		})
		e.text = str(value)
		# then lay down the text in the specified style
		e = etree.SubElement(self.foreground, 'text', {
			'x': str(x),
			'y': str(y),
			'class': 'dataPointLabel'})
		e.text = str(value)
		if style:
			e.set('style', style)

Response: Add text for a datapoint


Instruction:
Make a summary of the following Python 3 code
def draw_x_labels(self):
		"Draw the X axis labels"
		if self.show_x_labels:
			labels = self.get_x_labels()
			count = len(labels)

			labels = enumerate(iter(labels))
			start = int(not self.step_include_first_x_label)
			labels = itertools.islice(labels, start, None, self.step_x_labels)
			list(map(self.draw_x_label, labels))
			self.draw_x_guidelines(self.field_width(), count)

Response: Draw the X axis labels


Instruction:
How would you explain what the following Python 3 function does
def draw_y_labels(self):
		"Draw the Y axis labels"
		if not self.show_y_labels:
			# do nothing
			return

		labels = self.get_y_labels()
		count = len(labels)

		labels = enumerate(iter(labels))
		start = int(not self.step_include_first_y_label)
		labels = itertools.islice(labels, start, None, self.step_y_labels)
		list(map(self.draw_y_label, labels))
		self.draw_y_guidelines(self.field_height(), count)

Response: Draw the Y axis labels


Instruction:
Make a summary of the following Python 3 code
def draw_x_guidelines(self, label_height, count):
		"Draw the X-axis guidelines"
		if not self.show_x_guidelines:
			return
		# skip the first one
		for count in range(1, count):
			move = 'M {start} 0 v{stop}'.format(
				start=label_height * count,
				stop=self.graph_height,
			)
			path = {'d': move, 'class': 'guideLines'}
			etree.SubElement(self.graph, 'path', path)

Response: Draw the X - axis guidelines


Instruction:
Can you implement a function in Python 3 that
draws the Y - axis guidelines

Response:
def draw_y_guidelines(self, label_height, count):
		"Draw the Y-axis guidelines"
		if not self.show_y_guidelines:
			return
		for count in range(1, count):
			move = 'M 0 {start} h{stop}'.format(
				start=self.graph_height - label_height * count,
				stop=self.graph_width,
			)
			path = {'d': move, 'class': 'guideLines'}
			etree.SubElement(self.graph, 'path', path)


Instruction:
Create a Python 3 function for
drawing the graph title and subtitle

Response:
def draw_titles(self):
		"Draws the graph title and subtitle"
		if self.show_graph_title:
			self.draw_graph_title()
		if self.show_graph_subtitle:
			self.draw_graph_subtitle()
		if self.show_x_title:
			self.draw_x_title()
		if self.show_y_title:
			self.draw_y_title()


Instruction:
Can you tell what is the following Python 3 function doing
def render_inline_styles(self):
		"Hard-code the styles into the SVG XML if style sheets are not used."
		if not self.css_inline:
			# do nothing
			return

		styles = self.parse_css()
		for node in self.root.xpath('//*[@class]'):
			cl = '.' + node.attrib['class']
			if cl not in styles:
				continue
			style = styles[cl]
			if 'style' in node.attrib:
				style += node.attrib['style']
			node.attrib['style'] = style

Response: Hard - code the styles into the SVG XML if style sheets are not used.


Instruction:
Can you tell what is the following Python 3 function doing
def parse_css(self):
		# todo: save the prefs for use later
		# orig_prefs = cssutils.ser.prefs
		cssutils.ser.prefs.useMinified()
		pairs = (
			(r.selectorText, r.style.cssText)
			for r in self.get_stylesheet()
			if not isinstance(r, cssutils.css.CSSComment)
		)
		return dict(pairs)

Response: Take a. css file and parse it into a dictionary containing class and style pairs.


Instruction:
Create a Python 3 function for
basing SVG Document Creation

Response:
def start_svg(self):
		"Base SVG Document Creation"
		SVG_NAMESPACE = 'http://www.w3.org/2000/svg'
		SVG = '{%s}' % SVG_NAMESPACE
		NSMAP = {
			None: SVG_NAMESPACE,
			'xlink': 'http://www.w3.org/1999/xlink',
			'a3': 'http://ns.adobe.com/AdobeSVGViewerExtensions/3.0/',
		}
		root_attrs = self._get_root_attributes()
		self.root = etree.Element(SVG + "svg", attrib=root_attrs, nsmap=NSMAP)
		if hasattr(self, 'style_sheet_href'):
			pi = etree.ProcessingInstruction(
				'xml-stylesheet',
				'href="%s" type="text/css"' % self.style_sheet_href
			)
			self.root.addprevious(pi)

		comment_strings = (
			' Created with SVG.Graph ',
			' SVG.Graph by Jason R. Coombs ',
			' Based on SVG::Graph by Sean E. Russel ',
			' Based on Perl SVG:TT:Graph by Leo Lapworth & Stephan Morgan ',
			' ' + '/' * 66,
		)
		list(map(self.root.append, map(etree.Comment, comment_strings)))

		defs = etree.SubElement(self.root, 'defs')
		self.add_defs(defs)

		if not hasattr(self, 'style_sheet_href') and not self.css_inline:
			self.root.append(etree.Comment(
				' include default stylesheet if none specified '))
			style = etree.SubElement(defs, 'style', type='text/css')
			# TODO: the text was previously escaped in a CDATA declaration... how
			#  to do that with etree?
			style.text = self.get_stylesheet().cssText

		self.root.append(etree.Comment('SVG Background'))
		etree.SubElement(self.root, 'rect', {
			'width': str(self.width),
			'height': str(self.height),
			'x': '0',
			'y': '0',
			'class': 'svgBackground'})


Instruction:
Implement a Python 3 function for
getting the stylesheets for this instance

Response:
def get_stylesheet_resources(self):
		"Get the stylesheets for this instance"
		# allow css to include class variables
		class_vars = class_dict(self)
		loader = functools.partial(
			self.load_resource_stylesheet,
			subs=class_vars)
		sheets = list(map(loader, self.stylesheet_names))
		return sheets


Instruction:
Make a summary of the following Python 3 code
def send(self, data, force=False):
        if self._registered or force:
            self._sock_file.write('%s\r\n' % data)
            self._sock_file.flush()
        else:
            self._out_buffer.append(data)

Response: \ Sends data over the wire if the connection is registered.


Instruction:
Write a Python 3 function that can
connect to the IRC server using the nickname

Response:
def connect(self):
        """\
        Connect to the IRC server using the nickname
        """
        self._sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        if self.use_ssl:
            self._sock = ssl.wrap_socket(self._sock)
        try:
            self._sock.connect((self.server, self.port))
        except socket.error:
            self.logger.error('Unable to connect to %s on port %d' % (self.server, self.port), exc_info=1)
            return False

        self._sock_file = self._sock.makefile()
        if self.password:
            self.set_password()
        self.register_nick()
        self.register()
        return True


Instruction:
Can you implement a function in Python 3 that
sends a message to a specific user or channel.

Response:
def respond(self, message, channel=None, nick=None):
        """\
        Multipurpose method for sending responses to channel or via message to
        a single user
        """
        if channel:
            if not channel.startswith('#'):
                channel = '#%s' % channel
            self.send('PRIVMSG %s :%s' % (channel, message))
        elif nick:
            self.send('PRIVMSG %s :%s' % (nick, message))


Instruction:
Can you implement a function in Python 3 that
dispatches the patterns of the socket data based on the patterns in the patterns in the patterns list.

Response:
def dispatch_patterns(self):
        """\
        Low-level dispatching of socket data based on regex matching, in general
        handles

        * In event a nickname is taken, registers under a different one
        * Responds to periodic PING messages from server
        * Dispatches to registered callbacks when
            - any user leaves or enters a room currently connected to
            - a channel message is observed
            - a private message is received
        """
        return (
            (self.nick_re, self.new_nick),
            (self.nick_change_re, self.handle_nick_change),
            (self.ping_re, self.handle_ping),
            (self.part_re, self.handle_part),
            (self.join_re, self.handle_join),
            (self.quit_re, self.handle_quit),
            (self.chanmsg_re, self.handle_channel_message),
            (self.privmsg_re, self.handle_private_message),
            (self.registered_re, self.handle_registered),
        )


Instruction:
Can you generate the documentation for the following Python 3 function
def handle_registered(self, server):
        if not self._registered:
            self.logger.info('Registered')
            self._registered = True
            for data in self._out_buffer:
                self.send(data)
            self._out_buffer = []

Response: Handle a registered connection.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def register_callbacks(self):
        self.conn.register_callbacks((
            (re.compile(pattern), callback) \
                for pattern, callback in self.command_patterns()
        ))

Response: Register callbacks with the connection


Instruction:
How would you implement a function in Python 3 that
wraps for the conn. respond method.

Response:
def respond(self, message, channel=None, nick=None):
        """\
        Wraps the connection object's respond() method
        """
        self.conn.respond(message, channel, nick)


Instruction:
Here you have a function in Python 3, explain what it does
def register_with_boss(self):
        gevent.sleep(10) # wait for things to connect, etc

        while not self.registered.is_set():
            self.respond('!register {%s}' % platform.node(), nick=self.boss)
            gevent.sleep(30)

Response: Register the worker with the boss.


Instruction:
Given the following Python 3 function, write the documentation
def task_runner(self):
        while 1:
            (task_id, command) = self.task_queue.get()

            for pattern, callback in self.task_patterns:
                match = re.match(pattern, command)
                if match:
                    # execute the callback
                    ret = callback(**match.groupdict()) or ''

                    # clear the stop flag in the event it was set
                    self.stop_flag.clear()

                    # send output of command to channel
                    for line in ret.splitlines():
                        self.respond('!task-data %s:%s' % (task_id, line), self.channel)
                        gevent.sleep(.34)

            # indicate task is complete
            self.respond('!task-finished %s' % task_id, self.channel)

Response: This function is run in a greenlet and will pull the task queue and execute the task callbacks.


Instruction:
Explain what the following Python 3 code does
def require_boss(self, callback):
        def inner(nick, message, channel, *args, **kwargs):
            if nick != self.boss:
                return

            return callback(nick, message, channel, *args, **kwargs)
        return inner

Response: Decorator to ensure that the command can come from the boss.


Instruction:
How would you explain what the following Python 3 function does
def command_patterns(self):
        return (
            ('!register-success (?P<cmd_channel>.+)', self.require_boss(self.register_success)),
            ('!worker-execute (?:\((?P<workers>.+?)\) )?(?P<task_id>\d+):(?P<command>.+)', self.require_boss(self.worker_execute)),
            ('!worker-ping', self.require_boss(self.worker_ping_handler)),
            ('!worker-stop', self.require_boss(self.worker_stop)),
        )

Response: Return a list of command patterns that can be used to dispatch the command to the worker bot.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def register_success(self, nick, message, channel, cmd_channel):
        # the boss will tell what channel to join
        self.channel = cmd_channel
        self.conn.join(self.channel)

        # indicate that registered so we'll stop trying
        self.registered.set()

Response: This method is called when the bot is successfully registered with the command channel.


Instruction:
How would you code a function in Python 3 to
indicate that the worker with given nick is performing this task

Response:
def add(self, nick):
        """\
        Indicate that the worker with given nick is performing this task
        """
        self.data[nick] = ''
        self.workers.add(nick)


Instruction:
Explain what the following Python 3 code does
def send_validation_email(self):
        if self.email_verified:
            raise ValueError(_('Cannot validate already active user.'))

        site = Site.objects.get_current()
        self.validation_notification(user=self, site=site).notify()

Response: Send a validation email to the user s email address.


Instruction:
Given the following Python 3 function, write the documentation
def send_password_reset(self):
        site = Site.objects.get_current()
        self.password_reset_notification(user=self, site=site).notify()

Response: Send a password reset notification to the user s email address.


Instruction:
Can you tell what is the following Python 3 function doing
def validate_password_strength(value):
    used_chars = set(value)
    good_chars = set(ascii_letters + digits + punctuation + ' ')
    required_sets = (ascii_uppercase, ascii_lowercase, digits)

    if not used_chars.issubset(good_chars):
        raise ValidationError(too_fancy)

    for required in required_sets:
        if not used_chars.intersection(required):
            raise ValidationError(too_simple)

Response: Validate that the password strength is correct.