Instruction:
Write a Python 3 function for
flattening a dictionary into a single dictionary.

Response:
def flatten_dict(dic, parent_key='', sep='.'):
    """Flatten sub-keys of a dictionary
    """
    items = []
    for key, value in dic.items():
        new_key = parent_key + sep + key if parent_key else key
        if isinstance(value, collections.MutableMapping):
            items.extend(flatten_dict(value, new_key, sep=sep).items())
        elif isinstance(value, list):
            for idx, elt in enumerate(value):
                items.extend(
                    flatten_dict(elt, new_key + sep + str(idx), sep=sep).items()
                )
        else:
            items.append((new_key, value))
    return dict(items)


Instruction:
Here you have a function in Python 3, explain what it does
def dict_merge(dct, merge_dct):
    for key in merge_dct.keys():
        if (
            key in dct
            and isinstance(dct[key], dict)
            and isinstance(merge_dct[key], collections.Mapping)
        ):
            dict_merge(dct[key], merge_dct[key])
        else:
            dct[key] = merge_dct[key]

Response: Recursive dict merge. Inspired by dict. update.


Instruction:
Implement a Python 3 function for
transforming tree of dict and list in read - only data structure.

Response:
def freeze(obj):
    """Transform tree of dict and list in read-only
    data structure.
    dict instances are transformed to FrozenDict, lists
    in FrozenList.
    """
    if isinstance(obj, collections.Mapping):
        return FrozenDict({freeze(k): freeze(v) for k, v in six.iteritems(obj)})
    elif isinstance(obj, list):
        return FrozenList([freeze(e) for e in obj])
    else:
        return obj


Instruction:
Can you write a function in Python 3 where it
creates a Configuration from a YAML file.

Response:
def from_file(cls, path):
        """Create a ``Configuration`` from a file

        :param path: path to YAML file
        :return: new configuration
        :rtype: ``Configuration``
        """
        if path == '-':
            return Configuration(yaml.safe_load(sys.stdin))
        if not osp.exists(path) and not osp.isabs(path):
            path = osp.join(osp.dirname(osp.abspath(__file__)), path)
        with open(path, 'r') as istr:
            return Configuration(yaml.safe_load(istr))


Instruction:
Write a Python 3 script for
converting a hex - formatted number to an anatomical color tuple.

Response:
def hex_color_to_rgba(hex_color, normalize_to=255):
    '''
    Convert a hex-formatted number (i.e., `"#RGB[A]"` or `"#RRGGBB[AA]"`) to an
    RGBA tuple (i.e., `(<r>, <g>, <b>, <a>)`).

    Args:

        hex_color (str) : hex-formatted number (e.g., `"#2fc"`, `"#3c2f8611"`)
        normalize_to (int, float) : Factor to normalize each channel by

    Returns:

        (tuple) : RGBA tuple (i.e., `(<r>, <g>, <b>, <a>)`), where range of
            each channel in tuple is `[0, normalize_to]`.
    '''
    color_pattern_one_digit = (r'#(?P<R>[\da-fA-F])(?P<G>[\da-fA-F])'
                               r'(?P<B>[\da-fA-F])(?P<A>[\da-fA-F])?')
    color_pattern_two_digit = (r'#(?P<R>[\da-fA-F]{2})(?P<G>[\da-fA-F]{2})'
                               r'(?P<B>[\da-fA-F]{2})(?P<A>[\da-fA-F]{2})?')

    # First try to match `#rrggbb[aa]`.
    match = re.match(color_pattern_two_digit, hex_color)

    if match:
        channels = match.groupdict()
        channel_scale = 255
    else:
        # Try to match `#rgb[a]`.
        match = re.match(color_pattern_one_digit, hex_color)
        if match:
            channels = match.groupdict()
            channel_scale = 15
        else:
            raise ValueError('Color string must be in format #RGB[A] or '
                             '#RRGGBB[AA] (i.e., alpha channel is optional)')

    scale = normalize_to / channel_scale
    return tuple(type(normalize_to)(int(channels[k], 16) * scale)
                 if channels[k] is not None else None
                 for k in 'RGBA')


Instruction:
Can you implement a function in Python 3 that
splits a term into parent and record term components

Response:
def split_term(cls, term):
        """
        Split a term in to parent and record term components
        :param term: combined term text
        :return: Tuple of parent and record term
        """

        if '.' in term:
            parent_term, record_term = term.split('.')
            parent_term, record_term = parent_term.strip(), record_term.strip()

            if parent_term == '':
                parent_term = ELIDED_TERM

        else:
            parent_term, record_term = ROOT_TERM, term.strip()

        return parent_term, record_term


Instruction:
Given the following Python 3 function, write the documentation
def split_term_lower(cls, term):

        return tuple(e.lower() for e in Term.split_term(term))

Response: Like split_term but also lowercases both parent and record term


Instruction:
Can you create a Python 3 function that
returns a string for the file row and column of the term.

Response:
def file_ref(self):
        """Return a string for the file, row and column of the term."""
        from metatab.util import slugify

        assert self.file_name is None or isinstance(self.file_name, str)

        if self.file_name is not None and self.row is not None:
            parts = split(self.file_name);
            return "{} {}:{} ".format(parts[-1], self.row, self.col)
        elif self.row is not None:
            return " {}:{} ".format(self.row, self.col)
        else:
            return ''


Instruction:
Implement a function in Python 3 to
add a term to this term s children. Also sets the child term s parent.

Response:
def add_child(self, child):
        """Add a term to this term's children. Also sets the child term's parent"""
        assert isinstance(child, Term)
        self.children.append(child)
        child.parent = self
        assert not child.term_is("Datafile.Section")


Instruction:
Can you create a Python 3 function that
creates a new term and add it to this term as a child.

Response:
def new_child(self, term, value, **kwargs):
        """Create a new term and add it to this term as a child. Creates grandchildren from the kwargs.

        :param term: term name. Just the record term
        :param term: Value to assign to the term
        :param term: Term properties, which create children of the child term.

        """

        tc = self.doc.get_term_class(term.lower())

        c = tc(term, str(value) if value is not None else None,
               parent=self, doc=self.doc, section=self.section).new_children(**kwargs)

        c.term_value_name = self.doc.decl_terms.get(c.join, {}).get('termvaluename', c.term_value_name)

        assert not c.term_is("*.Section")
        self.children.append(c)
        return c


Instruction:
Can you generate a brief explanation for the following Python 3 code
def remove_child(self, child):
        assert isinstance(child, Term)
        self.children.remove(child)
        self.doc.remove_term(child)

Response: Removes the term from this term s children.


Instruction:
Can you tell what is the following Python 3 function doing
def new_children(self, **kwargs):
        for k, v in kwargs.items():
            self.new_child(k, v)

        return self

Response: Create new children from kwargs


Instruction:
Given the following Python 3 function, write the documentation
def set_ownership(self):
        assert self.section is not None

        for t in self.children:
            t.parent = self
            t._section = self.section
            t.doc = self.doc
            t.set_ownership()

Response: Recursivelt set the parent section and doc for a children


Instruction:
Can you tell what is the following Python 3 function doing
def find(self, term, value=False):
        if '.' in term:
            parent, term = term.split('.')
            assert parent.lower() == self.record_term_lc, (parent.lower(), self.record_term_lc)

        for c in self.children:
            if c.record_term_lc == term.lower():
                if value is False or c.value == value:
                    yield c

Response: Return a list of terms by name.


Instruction:
How would you code a function in Python 3 to
find a term using find_first and set it s value and properties and create a new child term. If it does not exist create a new child term.

Response:
def get_or_new_child(self, term, value=False, **kwargs):
        """Find a term, using find_first, and set it's value and properties, if it exists. If
        it does not, create a new term and children. """

        pt, rt = self.split_term(term)

        term = self.record_term + '.' + rt

        c = self.find_first(rt)

        if c is None:
            tc = self.doc.get_term_class(term.lower())
            c = tc(term, value, parent=self, doc=self.doc, section=self.section).new_children(**kwargs)
            assert not c.term_is("Datafile.Section"), (self, c)
            self.children.append(c)

        else:
            if value is not False:
                c.value = value

            for k, v in kwargs.items():
                c.get_or_new_child(k, v)

        # Check that the term was inserted and can be found.
        assert self.find_first(rt)
        assert self.find_first(rt) == c

        return c


Instruction:
Can you tell what is the following Python 3 function doing
def get_value(self, item, default=None):
        try:
            return self[item].value
        except (AttributeError, KeyError) as e:
            return default

Response: Get the value of a child


Instruction:
Implement a function in Python 3 to
return the fully qualified term name.

Response:
def qualified_term(self):
        """Return the fully qualified term name. The parent will be 'root' if there is no parent term defined. """

        assert self.parent is not None or self.parent_term_lc == 'root'

        if self.parent:
            return self.parent.record_term_lc + '.' + self.record_term_lc
        else:
            return 'root.' + self.record_term_lc


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

        if isinstance(v, str):

            if '.' not in v:
                v = 'root.' + v

            v_p, v_r = self.split_term_lower(v)

            if self.join_lc == v.lower():
                return True
            elif v_r == '*' and v_p == self.parent_term_lc:
                return True
            elif v_p == '*' and v_r == self.record_term_lc:
                return True
            elif v_p == '*' and v_r == '*':
                return True
            else:
                return False

        else:

            return any(self.term_is(e) for e in v)

Response: Return True if the fully qualified name of the term is the same as the argument.


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

        d = dict(zip([str(e).lower() for e in self.section.property_names], self.args))

        # print({ c.record_term_lc:c.value for c in self.children})
        d[self.term_value_name.lower()] = self.value

        return d

Response: Return the value and scalar properties as a dictionary. Returns only the value and scalar properties declared on the same row as a term. Returns only the value and scalar properties declared on the same row as a term.


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

        d = self.arg_props
        d.update(self.props)

        return d

Response:
Returns a dictionary with the values of all children and place holders for all of the section
        argumemts. It combines arg_props with props


Instruction:
Can you generate the documentation for the following Python 3 function
def _convert_to_dict(cls, term, replace_value_names=True):

        from collections import OrderedDict

        if not term:
            return None

        if term.children:

            d = OrderedDict()

            for c in term.children:

                if c.child_property_type == 'scalar':
                    d[c.record_term_lc] = cls._convert_to_dict(c, replace_value_names)

                elif c.child_property_type == 'sequence':
                    try:
                        d[c.record_term_lc].append(cls._convert_to_dict(c, replace_value_names))
                    except (KeyError, AttributeError):
                        # The c.term property doesn't exist, so add a list
                        d[c.record_term_lc] = [cls._convert_to_dict(c, replace_value_names)]

                elif c.child_property_type == 'sconcat': # Concat with a space

                    if c.record_term_lc in d:
                        s = d[c.record_term_lc] + ' '
                    else:
                        s = ''

                    d[c.record_term_lc] =s + (cls._convert_to_dict(c, replace_value_names) or '')

                elif c.child_property_type == 'bconcat':  # Concat with a blank

                    d[c.record_term_lc] = d.get(c.record_term_lc, '') +  (cls._convert_to_dict(c,
                                                                                replace_value_names)
                                                                          or '')

                else:
                    try:
                        d[c.record_term_lc].append(cls._convert_to_dict(c, replace_value_names))
                    except KeyError:
                        # The c.term property doesn't exist, so add a scalar or a map
                        d[c.record_term_lc] = cls._convert_to_dict(c, replace_value_names)
                    except AttributeError as e:
                        # d[c.term] exists, but is a scalar, so convert it to a list

                        d[c.record_term_lc] = [d[c.record_term]] + [cls._convert_to_dict(c, replace_value_names)]

            if term.value:
                if replace_value_names:
                    d[term.term_value_name.lower()] = term.value
                else:
                    d['@value'] = term.value

            return d

        else:
            return term.value

Response: Converts a record heirarchy to nested dicts.


Instruction:
Create a Python 3 function for
yielding the rows for the term for writing terms to a CSV file.

Response:
def rows(self):
        """Yield rows for the term, for writing terms to a CSV file. """

        # Translate the term value name so it can be assigned to a parameter.
        tvm = self.section.doc.decl_terms.get(self.qualified_term, {}).get('termvaluename', '@value')
        assert tvm
        # Terminal children have no arguments, just a value. Here we put the terminal children
        # in a property array, so they can be written to the parent's arg-children columns
        # if the section has any.

        properties = {tvm: self.value}

        for c in self.children:
            if c.is_terminal:
                if c.record_term_lc:
                    # This is rare, but can happen if a property child is not given
                    # a property name by the section -- the "Section" term has a blank column.
                    properties[c.record_term_lc] = c.value

        yield (self.qualified_term, properties)

        # The non-terminal children have to get yielded normally -- they can't be arg-children
        for c in self.children:
            if not c.is_terminal:
                for row in c.rows:
                    yield row


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

        for c in self.children:
            yield c

            for d in c.descendents:
                yield d

Response: Iterate over all descendent terms


Instruction:
Explain what the following Python 3 code does
def subclass(cls, t):
        t.doc = None
        t.terms = []
        t.__class__ = SectionTerm
        return t

Response: Change a term into a Section Term


Instruction:
How would you explain what the following Python 3 function does
def add_arg(self,arg, prepend=False):

        self.args = [arg_.strip() for arg_ in self.args if arg_.strip()]

        if arg.title() not in self.args:
            if prepend:
                self.args = [arg.title()] + self.args
            else:
                self.args.append(arg.title())

Response: Append an arg to the arg list


Instruction:
Explain what the following Python 3 code does
def remove_arg(self, arg):

        self.args = [arg_.strip() for arg_ in self.args if arg_.strip()]

        for arg_ in list(self.args):
            if arg_.lower() == arg.lower():
                self.args.remove(arg_)

Response: Remove an arg from the arg list


Instruction:
Can you tell what is the following Python 3 function doing
def add_term(self, t):
        if t not in self.terms:
            if t.parent_term_lc == 'root':
                self.terms.append(t)

                self.doc.add_term(t, add_section=False)

                t.set_ownership()

            else:
                raise GenerateError("Can only add or move root-level terms. Term '{}' parent is '{}' "
                                    .format(t, t.parent_term_lc))

        assert t.section or t.join_lc == 'root.root', t

Response: Add a term to this section and set it s ownership. Should only be used on root level terms


Instruction:
Can you generate a brief explanation for the following Python 3 code
def new_term(self, term, value, **kwargs):

        tc = self.doc.get_term_class(term.lower())

        t = tc(term, value, doc=self.doc, parent=None, section=self).new_children(**kwargs)

        self.doc.add_term(t)
        return t

Response: Create a new root - level term in this section


Instruction:
Implement a function in Python 3 to
create or set top level terms in the section.

Response:
def set_terms(self,*terms, **kw_terms):
        """
        Create or set top level terms in the section. After python 3.6.0, the terms entries
        should maintain the same order as the argument list. The term arguments can have any of these forms:

        * For position argument, a Term object
        * For kw arguments:
            - 'TermName=TermValue'
            - 'TermName=(TermValue, PropertyDict)

        Positional arguments are processed before keyword arguments, and are passed into .add_term()

        :param terms: Term arguments
        :return:
        """

        for t in terms:
            self.add_term(t)

        for k,v in kw_terms.items():
            try:
                value, props = v
            except (ValueError, TypeError) as e:
                value, props = v,{}

            self.new_term(k,value,**props)


Instruction:
Can you generate the documentation for the following Python 3 function
def remove_term(self, term, remove_from_doc=True):

        try:
            self.terms.remove(term)
        except ValueError:
            pass

        if remove_from_doc:
            self.doc.remove_term(term)

Response: Remove a term from the terms. Must be the identical term the same object.


Instruction:
Given the following Python 3 function, write the documentation
def clean(self):
        terms = list(self)

        for t in terms:
            self.doc.remove_term(t)

Response: Remove all of the terms from the section and also remove them from the document


Instruction:
How would you code a function in Python 3 to
sort the terms in the section.

Response:
def sort_by_term(self, order=None):
        """
        Sort the terms in the section.
        :param order: If specified, a list of qualified, lowercased term names. These names will appear
        first in the section, and the remaining terms will be sorted alphabetically. If not specified, all terms are
        alphabetized.
        :return:
        """

        if order is None:
            self.terms = sorted(self.terms, key=lambda e: e.join_lc)
        else:

            all_terms = list(self.terms)
            sorted_terms = []

            for tn in order:
                for t in list(all_terms):

                    if t.term_is(tn):
                        all_terms.remove(t)
                        sorted_terms.append(t)

            sorted_terms.extend(sorted(all_terms, key=lambda e: e.join_lc))

            self.terms = sorted_terms


Instruction:
Make a summary of the following Python 3 code
def _args(self, term, d):

        # Get the term value name, the property name that should be assigned to the term value.
        tvm = self.doc.decl_terms.get(term, {}).get('termvaluename', '@value')

        # Convert the keys to lower case

        lower_d = {k.lower(): v for k, v in d.items()}

        args = []
        for n in [tvm] + self.property_names:
            args.append(lower_d.get(n.lower(), ''))

            try:
                del lower_d[n.lower()]
            except KeyError:
                pass

        return term, args, lower_d

Response: Extract the chldren of a term that are arg - children from those that are row - children.


Instruction:
Create a Python 3 function to
yield the rows for the section

Response:
def rows(self):
        """Yield rows for the section"""
        for t in self.terms:

            for row in t.rows:
                term, value = row  # Value can either be a string, or a dict

                if isinstance(value, dict):  # Dict is for properties, which might be arg-children
                    term, args, remain = self._args(term, value)
                    yield term, args

                    # 'remain' is all of the children that didn't have an arg-child column -- the
                    # section didn't have a column heder for that ther.
                    for k, v in remain.items():
                        yield term.split('.')[-1] + '.' + k, v
                else:
                    yield row


Instruction:
How would you code a function in Python 3 to
iterate over all of the rows as text lines

Response:
def lines(self):
        """Iterate over all of the rows as text lines"""

        # Yield the section header
        if self.name != 'Root':
            yield ('Section', '|'.join([self.value] + self.property_names))

        # Yield all of the rows for terms in the section
        for row in self.rows:
            term, value = row

            if not isinstance(value, (list, tuple)):
                value = [value]

            term = term.replace('root.', '').title()
            yield (term, value[0])

            children = list(zip(self.property_names, value[1:]))

            for prop, value in children:
                if value and value.strip():
                    child_t = '.' + (prop.title())
                    yield ("    "+child_t, value)


Instruction:
Write a Python 3 script to
return the whole section as a dict

Response:
def as_dict(self, replace_value_names=True):
        """Return the whole section as a dict"""
        old_children = self.children
        self.children = self.terms

        d = super(SectionTerm, self).as_dict(replace_value_names)

        self.children = old_children

        return d


Instruction:
Can you tell what is the following Python 3 function doing
def find_compiler(cls, preferred_vendor, metadir, cwd,
                      use_meta=True):
        """
        Identify a suitable C/fortran/other compiler

        When it is possible that the user (un)installs a compiler
        inbetween compilations of object files we want to catch
        that. This method allows compiler choice to be stored in a
        pickled metadata file.  Provide metadir a dirpath to
        make the class save choice there in a file with
        cls.metadata_filename as name.
        """
        cwd = cwd or '.'
        metadir = metadir or '.'
        metadir = os.path.join(cwd, metadir)
        used_metafile = False
        if not preferred_vendor and use_meta:
            try:
                preferred_vendor = cls.get_from_metadata_file(
                    metadir, 'vendor')
                used_metafile = True
            except FileNotFoundError:
                pass
        candidates = list(cls.compiler_dict.keys())
        if preferred_vendor:
            if preferred_vendor in candidates:
                candidates = [preferred_vendor]+candidates
            else:
                raise ValueError("Unknown vendor {}".format(
                    preferred_vendor))
        name, path = find_binary_of_command([
            cls.compiler_dict[x] for x in candidates])
        if use_meta and not used_metafile:
            if not os.path.isdir(metadir):
                raise FileNotFoundError("Not a dir: {}".format(metadir))
            cls.save_to_metadata_file(metadir, 'compiler',
                                      (name, path))
            cls.save_to_metadata_file(
                metadir, 'vendor',
                cls.compiler_name_vendor_mapping[name])
            if cls.logger:
                cls.logger.info(
                    'Wrote choice of compiler to: metadir')
        return name, path, cls.compiler_name_vendor_mapping[name]

Response: Find a suitable C compiler and return its name path and vendor.


Instruction:
Write a Python 3 function that can
return the command to run this module.

Response:
def cmd(self):
        """
        The command below covers most cases, if you need
        someting more complex subclass this.
        """
        cmd = (
            [self.compiler_binary] +
            self.flags +
            ['-U'+x for x in self.undef] +
            ['-D'+x for x in self.define] +
            ['-I'+x for x in self.include_dirs] +
            self.sources
        )
        if self.run_linker:
            cmd += (['-L'+x for x in self.library_dirs] +
                    [(x if os.path.exists(x) else '-l'+x) for x in self.libraries] +
                    self.linkline)
        counted = []
        for envvar in re.findall('\$\{(\w+)\}', ' '.join(cmd)):
            if os.getenv(envvar) is None:
                if envvar not in counted:
                    counted.append(envvar)
                    msg = "Environment variable '{}' undefined.".format(
                        envvar)
                    self.logger.error(msg)
                    raise CompilationError(msg)
        return cmd


Instruction:
Create a Python 3 function for
drawing a line on the current layer with the current brush.

Response:
def drawLine(self, x1, y1, x2, y2, silent=False):
		"""
		Draws a line on the current :py:class:`Layer` with the current :py:class:`Brush`.
		Coordinates are relative to the original layer size WITHOUT downsampling applied.

		:param x1: Starting X coordinate.
		:param y1: Starting Y coordinate.
		:param x2: End X coordinate.
		:param y2: End Y coordinate.
		:rtype: Nothing.
		"""
		start = time.time()

		#Downsample the coordinates
		x1 = int(x1/config.DOWNSAMPLING)
		x2 = int(x2/config.DOWNSAMPLING)
		y1 = int(y1/config.DOWNSAMPLING)
		y2 = int(y2/config.DOWNSAMPLING)
		if not silent :
			print('drawing line from: '+str((x1,y1))+' to: '+str((x2,y2)))

		#Calculate the direction and the length of the step
		direction = N.arctan2(y2 - y1, x2 - x1)
		length = self.brush.spacing

		#Prepare the loop
		x, y = x1, y1
		totalSteps = int(N.sqrt((x2 - x)**2 + (y2 - y)**2)/length)
		lay = self.image.getActiveLayer()
		col = self.color
		secCol = self.secondColor
		mirr = self.mirrorMode

		#If I use source caching..
		if self.brush.usesSourceCaching:
			#..than optimize it for faster drawing
			laydata = lay.data
			x -= self.brush.brushSize*0.5
			y -= self.brush.brushSize*0.5
			colbrsource = self.brush.coloredBrushSource
			canvSize = config.CANVAS_SIZE
			brmask = self.brush.brushMask
			for _ in range(totalSteps):
				#Make the dab on this point
				applyMirroredDab_jit(mirr, laydata, int(x), int(y), colbrsource.copy(), canvSize, brmask)

				#Mode the point for the next step and update the distances
				x += lendir_x(length, direction)
				y += lendir_y(length, direction)
		#..if I don't use source caching..
		else:
			#..do the normal drawing
			for _ in range(totalSteps):
				#Make the dab on this point
				self.brush.makeDab(lay, int(x), int(y), col, secCol, mirror=mirr)

				#Mode the point for the next step and update the distances
				x += lendir_x(length, direction)
				y += lendir_y(length, direction)


Instruction:
Make a summary of the following Python 3 code
def drawPoint(self, x, y, silent=True):
		start = time.time()

		#Downsample the coordinates
		x = int(x/config.DOWNSAMPLING)
		y = int(y/config.DOWNSAMPLING)

		#Apply the dab with or without source caching
		if self.brush.usesSourceCaching:
			applyMirroredDab_jit(self.mirrorMode, self.image.getActiveLayer().data, int(x-self.brush.brushSize*0.5), int(y-self.brush.brushSize*0.5), self.brush.coloredBrushSource.copy(), config.CANVAS_SIZE, self.brush.brushMask)
		else:
			self.brush.makeDab(self.image.getActiveLayer(), int(x), int(y), self.color, self.secondColor, mirror=self.mirrorMode)
		config.AVGTIME.append(time.time()-start)

Response: Draws a point on the current layer with the current brush.


Instruction:
Can you create a Python 3 function that
draws a series of lines on the current layer with the current brush.

Response:
def drawPath(self, pointList):
		"""
		Draws a series of lines on the current :py:class:`Layer` with the current :py:class:`Brush`.
		No interpolation is applied to these point and :py:meth:`drawLine` will be used to connect all the points lineraly.
		Coordinates are relative to the original layer size WITHOUT downsampling applied.

		:param pointList: A list of point like :code:`[(0, 0), (100, 100), (100, 200)]`.
		:rtype: Nothing.
		"""
		self.drawLine(pointList[0][0], pointList[0][1], pointList[1][0], pointList[1][1])
		i = 1
		while i<len(pointList)-1:
			self.drawLine(pointList[i][0], pointList[i][1], pointList[i+1][0], pointList[i+1][1])
			i+=1


Instruction:
Explain what the following Python 3 code does
def drawClosedPath(self, pointList):
		self.drawLine(pointList[0][0], pointList[0][1], pointList[1][0], pointList[1][1])
		i = 1
		while i<len(pointList)-1:
			self.drawLine(pointList[i][0], pointList[i][1], pointList[i+1][0], pointList[i+1][1])
			i+=1
		self.drawLine(pointList[-1][0], pointList[-1][1], pointList[0][0], pointList[0][1])

Response: Draws a closed series of lines on the current layer with the current brush.


Instruction:
Explain what the following Python 3 code does
def drawRect(self, x1, y1, x2, y2, angle=0):
		vertices = [[x1,y1],[x2,y1],[x2,y2],[x1,y2],]
		rotatedVertices = rotateMatrix(vertices, (x1+x2)*0.5, (y1+y2)*0.5, angle)
		self.drawClosedPath(rotatedVertices)

Response: Draws a rectangle on the current layer with the current brush.


Instruction:
How would you explain what the following Python 3 function does
def fillLayerWithColor(self, color):
		layer = self.image.getActiveLayer().data
		colorRGBA = color.get_0_255()
		layer[:,:,0] = colorRGBA[0]
		layer[:,:,1] = colorRGBA[1]
		layer[:,:,2] = colorRGBA[2]
		layer[:,:,3] = colorRGBA[3]

Response: Fills the current layer with the current color.


Instruction:
How would you code a function in Python 3 to
add a border to the current layer.

Response:
def addBorder(self, width, color=None):
		"""
		Add a border to the current :py:class:`Layer`.

		:param width: The width of the border.
		:param color: The :py:class:`Color` of the border, current :py:class:`Color` is the default value.
		:rtype: Nothing.
		"""
		width = int(width/config.DOWNSAMPLING)
		if color==None:
			color = self.color
		layer = self.image.getActiveLayer().data
		colorRGBA = color.get_0_255()
		print('adding border'+str(colorRGBA)+str(width)+str(layer.shape))
		layer[0:width,:,0] = colorRGBA[0]
		layer[0:width,:,1] = colorRGBA[1]
		layer[0:width,:,2] = colorRGBA[2]
		layer[0:width,:,3] = colorRGBA[3]

		layer[:,0:width,0] = colorRGBA[0]
		layer[:,0:width,1] = colorRGBA[1]
		layer[:,0:width,2] = colorRGBA[2]
		layer[:,0:width,3] = colorRGBA[3]

		layer[layer.shape[0]-width:layer.shape[0],:,0] = colorRGBA[0]
		layer[layer.shape[0]-width:layer.shape[0],:,1] = colorRGBA[1]
		layer[layer.shape[0]-width:layer.shape[0],:,2] = colorRGBA[2]
		layer[layer.shape[0]-width:layer.shape[0],:,3] = colorRGBA[3]

		layer[:,layer.shape[1]-width:layer.shape[1],0] = colorRGBA[0]
		layer[:,layer.shape[1]-width:layer.shape[1],1] = colorRGBA[1]
		layer[:,layer.shape[1]-width:layer.shape[1],2] = colorRGBA[2]
		layer[:,layer.shape[1]-width:layer.shape[1],3] = colorRGBA[3]


Instruction:
Write a Python 3 script for
setting the current color to use.

Response:
def setColor(self, color):
		"""
		Sets the current :py:class:`Color` to use.

		:param color: The :py:class:`Color` to use.
		:rtype: Nothing.
		"""
		self.color = color
		if self.brush and self.brush.doesUseSourceCaching():
			self.brush.cacheBrush(color)


Instruction:
Make a summary of the following Python 3 code
def setColorAlpha(self, fixed=None, proportional=None):
		if fixed!=None:
			self.color.set_alpha(fixed)
		elif proportional!=None:
			self.color.set_alpha(self.color.get_alpha()*proportional)

Response: Change the alpha of the current color.


Instruction:
Make a summary of the following Python 3 code
def swapColors(self):
		rgba = self.color.get_0_255()
		self.color = self.secondColor
		self.secondColor = Color(rgba, '0-255')

Response: Swaps the current Color with the secondary Color.


Instruction:
How would you code a function in Python 3 to
set the size of the current brush.

Response:
def setBrush(self, b, resize=0, proportional=None):
		"""
		Sets the size of the current :py:class:`Brush`.

		:param brush: The :py:class:`Brush` object to use as a brush.
		:param resize: An optional absolute value to resize the brush before using it.
		:param proportional: An optional relative float 0-1 value to resize the brush before using it.
		:rtype: Nothing.
		"""
		if proportional!=None:
			resize = int(self.brush.brushSize*0.5)
		b.resizeBrush(resize) #If resize=0 it reset to its default size
		self.brush = b
		if self.brush and self.brush.doesUseSourceCaching():
			self.brush.cacheBrush(self.color)


Instruction:
Here you have a function in Python 3, explain what it does
def setMirrorMode(self, mirror):
		assert (mirror=='' or mirror=='h' or mirror=='v' or mirror=='hv'or mirror=='vh'), 'setMirrorMode: wrong mirror mode, got '+str(mirror)+' expected one of ["","h","v","hv"]'

		#Round up all the coordinates and convert them to int
		if mirror=='': 		mirror = 0
		elif mirror=='h': 	mirror = 1
		elif mirror=='v': 	mirror = 2
		elif mirror=='hv': 	mirror = 3
		elif mirror=='vh': 	mirror = 3
		self.mirrorMode = mirror

Response: Sets the mirror mode for this object.


Instruction:
Write a Python 3 function that can
render the Image and outputs the final PNG file.

Response:
def renderImage(self, output='', show=True):
		"""
		Renders the :py:class:`Image` and outputs the final PNG file.

		:param output: A string with the output file path, can be empty if you don't want to save the final image.
		:param show: A boolean telling the system to display the final image after the rendering is done.
		:rtype: Nothing.
		"""
		#Merge all the layers to apply blending modes
		resultLayer = self.image.mergeAllLayers()

		#Show and save the results
		img = PIL.Image.fromarray(resultLayer.data, 'RGBA')

		if show:
			img.show()

		if output!='':
			img.save(output, 'PNG')


Instruction:
Create a Python 3 function to
change the effect of the current active layer.

Response:
def setActiveLayerEffect(self, effect):
		"""
		Changes the effect of the current active :py:class:`Layer`.

		:param output: A string with the one of the blend modes listed in :py:meth:`newLayer`.
		:rtype: Nothing.
		"""
		self.image.layers[self.image.activeLayer].effect = effect


Instruction:
Create a Python 3 function for
returning the hash of all the contents of the given directory.

Response:
def get_hash_of_dirs(directory):
    """
    Recursively hash the contents of the given directory.

    Args:
        directory (str): The root directory we want to hash.

    Returns:
        A hash of all the contents in the directory.
    """
    import hashlib
    sha = hashlib.sha512()
    if not os.path.exists(directory):
        return -1

    for root, _, files in os.walk(directory):
        for name in files:
            filepath = local.path(root) / name
            if filepath.exists():
                with open(filepath, 'rb') as next_file:
                    for line in next_file:
                        sha.update(line)
    return sha.hexdigest()


Instruction:
Make a summary of the following Python 3 code
def source_required(src_file):
    if not src_file.exists():
        return True

    required = True
    hash_file = src_file.with_suffix(".hash", depth=0)
    LOG.debug("Hash file location: %s", hash_file)
    if hash_file.exists():
        new_hash = get_hash_of_dirs(src_file)
        with open(hash_file, 'r') as h_file:
            old_hash = h_file.readline()
        required = not new_hash == old_hash
        if required:
            from benchbuild.utils.cmd import rm
            rm("-r", src_file)
            rm(hash_file)
    if required:
        LOG.info("Source required for: %s", src_file)
        LOG.debug("Reason: src-exists: %s hash-exists: %s", src_file.exists(),
                  hash_file.exists())
    return required

Response: Check if a download is required.


Instruction:
Can you create a Python 3 function that
updates the hash for the given file.

Response:
def update_hash(src_file):
    """
    Update the hash for the given file.

    Args:
        src: The file name.
        root: The path of the given file.
    """
    hash_file = local.path(src_file) + ".hash"
    new_hash = 0
    with open(hash_file, 'w') as h_file:
        new_hash = get_hash_of_dirs(src_file)
        h_file.write(str(new_hash))
    return new_hash


Instruction:
Can you generate the documentation for the following Python 3 function
def Copy(From, To):
    from benchbuild.utils.cmd import cp
    cp("-ar", "--reflink=auto", From, To)

Response:
Small copy wrapper.

    Args:
        From (str): Path to the SOURCE.
        To (str): Path to the TARGET.


Instruction:
How would you implement a function in Python 3 that
copies a file into the current working directory.

Response:
def CopyNoFail(src, root=None):
    """
    Just copy fName into the current working directory, if it exists.

    No action is executed, if fName does not exist. No Hash is checked.

    Args:
        src: The filename we want to copy to '.'.
        root: The optional source dir we should pull fName from. Defaults
            to benchbuild.settings.CFG["tmpdir"].

    Returns:
        True, if we copied something.
    """
    if root is None:
        root = str(CFG["tmp_dir"])
    src_path = local.path(root) / src

    if src_path.exists():
        Copy(src_path, '.')
        return True
    return False


Instruction:
Implement a Python 3 function for
downloading a file from a URL and update the hash of it.

Response:
def Wget(src_url, tgt_name, tgt_root=None):
    """
    Download url, if required.

    Args:
        src_url (str): Our SOURCE url.
        tgt_name (str): The filename we want to have on disk.
        tgt_root (str): The TARGET directory for the download.
            Defaults to ``CFG["tmpdir"]``.
    """
    if tgt_root is None:
        tgt_root = str(CFG["tmp_dir"])

    from benchbuild.utils.cmd import wget

    tgt_file = local.path(tgt_root) / tgt_name
    if not source_required(tgt_file):
        Copy(tgt_file, ".")
        return

    wget(src_url, "-O", tgt_file)
    update_hash(tgt_file)
    Copy(tgt_file, ".")


Instruction:
Can you implement a function in Python 3 that
decorates a project class with wget-based version information. This adds two attributes to a project class: - A `versions` method that returns a list of available versions for this project. - A `repository` attribute that provides a repository string to download from later. We use the `git rev-list` subcommand to list available versions. Args: url_dict (dict): A dictionary that assigns a version to a download URL. target_file (str): An optional path where we should put the clone. If unspecified, we will use the `SRC_FILE` attribute of the decorated class.

Response:
def with_wget(url_dict=None, target_file=None):
    """
    Decorate a project class with wget-based version information.

    This adds two attributes to a project class:
        - A `versions` method that returns a list of available versions
          for this project.
        - A `repository` attribute that provides a repository string to
          download from later.
    We use the `git rev-list` subcommand to list available versions.

    Args:
        url_dict (dict): A dictionary that assigns a version to a download URL.
        target_file (str): An optional path where we should put the clone.
            If unspecified, we will use the `SRC_FILE` attribute of
            the decorated class.
    """

    def wget_decorator(cls):
        def download_impl(self):
            """Download the selected version from the url_dict value."""
            t_file = target_file if target_file else self.SRC_FILE
            t_version = url_dict[self.version]
            Wget(t_version, t_file)

        @staticmethod
        def versions_impl():
            """Return a list of versions from the url_dict keys."""
            return list(url_dict.keys())

        cls.versions = versions_impl
        cls.download = download_impl
        return cls

    return wget_decorator


Instruction:
Can you tell what is the following Python 3 function doing
def Git(repository, directory, rev=None, prefix=None, shallow_clone=True):
    repository_loc = str(prefix)
    if prefix is None:
        repository_loc = str(CFG["tmp_dir"])

    from benchbuild.utils.cmd import git

    src_dir = local.path(repository_loc) / directory
    if not source_required(src_dir):
        Copy(src_dir, ".")
        return

    extra_param = []
    if shallow_clone:
        extra_param.append("--depth")
        extra_param.append("1")

    git("clone", extra_param, repository, src_dir)
    if rev:
        with local.cwd(src_dir):
            git("checkout", rev)

    update_hash(src_dir)
    Copy(src_dir, ".")
    return repository_loc

Response: Get a clone of the given git repo.


Instruction:
How would you explain what the following Python 3 function does
def with_git(repo,
             target_dir=None,
             limit=None,
             refspec="HEAD",
             clone=True,
             rev_list_args=None,
             version_filter=lambda version: True):
    """
    Decorate a project class with git-based version information.

    This adds two attributes to a project class:
        - A `versions` method that returns a list of available versions
          for this project.
        - A `repository` attribute that provides a repository string to
          download from later.
    We use the `git rev-list` subcommand to list available versions.

    Args:
        repo (str): Repository to download from, this will be stored
            in the `repository` attribute of the decorated class.
        target_dir (str): An optional path where we should put the clone.
            If unspecified, we will use the `SRC_FILE` attribute of
            the decorated class.
        limit (int): Limit the number of commits to consider for available
            versions. Versions are 'ordered' from latest to oldest.
        refspec (str): A git refspec string to start listing the versions from.
        clone (bool): Should we clone the repo if it isn't already available
            in our tmp dir? Defaults to `True`. You can set this to False to
            avoid time consuming clones, when the project has not been accessed
            at least once in your installation.
        ref_list_args (list of str): Additional arguments you want to pass to
            `git rev-list`.
        version_filter (class filter): Filter function to remove unwanted
            project versions.

    """
    if not rev_list_args:
        rev_list_args = []

    def git_decorator(cls):
        from benchbuild.utils.cmd import git

        @staticmethod
        def versions_impl():
            """Return a list of versions from the git hashes up to :limit:."""
            directory = cls.SRC_FILE if target_dir is None else target_dir
            repo_prefix = local.path(str(CFG["tmp_dir"]))
            repo_loc = local.path(repo_prefix) / directory
            if source_required(repo_loc):
                if not clone:
                    return []
                git("clone", repo, repo_loc)
                update_hash(repo_loc)

            with local.cwd(repo_loc):
                rev_list = git("rev-list", "--abbrev-commit", "--abbrev=10",
                               refspec, *rev_list_args).strip().split('\n')
                latest = git("rev-parse", "--short=10",
                             refspec).strip().split('\n')
                cls.VERSION = latest[0]

            if limit:
                return list(filter(version_filter, rev_list))[:limit]

            return list(filter(version_filter, rev_list))

        def download_impl(self):
            """Download the selected version."""
            nonlocal target_dir, git
            directory = cls.SRC_FILE if target_dir is None else target_dir
            Git(self.repository, directory)
            with local.cwd(directory):
                git("checkout", self.version)

        cls.versions = versions_impl
        cls.download = download_impl
        cls.repository = repo
        return cls

    return git_decorator

Response: Decorator for creating a new project class with git - based version information.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def Rsync(url, tgt_name, tgt_root=None):
    if tgt_root is None:
        tgt_root = str(CFG["tmp_dir"])

    from benchbuild.utils.cmd import rsync

    tgt_dir = local.path(tgt_root) / tgt_name
    if not source_required(tgt_dir):
        Copy(tgt_dir, ".")
        return

    rsync("-a", url, tgt_dir)
    update_hash(tgt_dir)
    Copy(tgt_dir, ".")

Response:
RSync a folder.

    Args:
        url (str): The url of the SOURCE location.
        fname (str): The name of the TARGET.
        to (str): Path of the target location.
            Defaults to ``CFG["tmpdir"]``.


Instruction:
Implement a Python 3 function for
converting string from camel case to snake case.

Response:
def camel_to_snake(s: str) -> str:
    """Convert string from camel case to snake case."""

    return CAMEL_CASE_RE.sub(r'_\1', s).strip().lower()


Instruction:
Make a summary of the following Python 3 code
def snake_to_camel(s: str) -> str:

    fragments = s.split('_')

    return fragments[0] + ''.join(x.title() for x in fragments[1:])

Response: Convert string from snake case to camel case.


Instruction:
Create a Python 3 function to
build metrics extractors according to the metrics section

Response:
def _create_extractors(cls, metrics):
        """Build metrics extractors according to the `metrics` config

        :param metrics: Benchmark `metrics` configuration section
        """
        metrics_dict = {}
        # group entries by `category` attribute (default is "standard")
        for metric, config in six.iteritems(metrics):
            category = config.get('category', StdBenchmark.DEFAULT_CATEGORY)
            metrics_dict.setdefault(category, {})[metric] = config
        # create one StdExtractor instance per category,
        # passing associated metrics
        return dict(
            (category, StdExtractor(metrics))
            for category, metrics in six.iteritems(metrics_dict)
        )


Instruction:
How would you explain what the following Python 3 function does
def metrics(self):
        return dict(
            (name, getattr(Metrics, config['type']))
            for name, config in six.iteritems(self._metrics)
        )

Response: Returns a dictionary of metrics extracted by this class


Instruction:
Here you have a function in Python 3, explain what it does
def froms(self):
        eax = {}
        for name, config in six.iteritems(self._metrics):
            from_ = self._get_property(config, 'from', default=self.stdout)
            eax.setdefault(from_, {})[name] = config
        return eax

Response: Group metrics according to the from property.


Instruction:
Can you generate the documentation for the following Python 3 function
def tesselate_shapes_frame(df_shapes, shape_i_columns):
    '''
    Tesselate each shape path into one or more triangles.

    Parameters
    ----------
    df_shapes : pandas.DataFrame
        Table containing vertices of shapes, one row per vertex, with the *at
        least* the following columns:
         - ``x``: The x-coordinate of the vertex.
         - ``y``: The y-coordinate of the vertex.
    shape_i_columns : str or list
        Column(s) forming key to differentiate rows/vertices for each distinct
        shape.

    Returns
    -------
    pandas.DataFrame

    Table where each row corresponds to a triangle vertex, with the following
    columns:

     - ``shape_i_columns[]``: The shape path index column(s).
     - ``triangle_i``: The integer triangle index within each electrode path.
     - ``vertex_i``: The integer vertex index within each triangle.
    '''
    frames = []
    if isinstance(shape_i_columns, bytes):
        shape_i_columns = [shape_i_columns]

    for shape_i, df_path in df_shapes.groupby(shape_i_columns):
        points_i = df_path[['x', 'y']].values
        if (points_i[0] == points_i[-1]).all():
            # XXX End point is the same as the start point (do not include it).
            points_i = points_i[:-1]
        try:
            triangulator = Triangulator(points_i)
        except:
            import pdb; pdb.set_trace()
            continue
        if not isinstance(shape_i, (list, tuple)):
            shape_i = [shape_i]

        for i, triangle_i in enumerate(triangulator.triangles()):
            triangle_points_i = [shape_i + [i] + [j, x, y]
                                 for j, (x, y) in enumerate(triangle_i)]
            frames.extend(triangle_points_i)
    frames = None if not frames else frames
    return pd.DataFrame(frames, columns=shape_i_columns +
                        ['triangle_i', 'vertex_i', 'x', 'y'])

Response: Tesselate each shape path into one or more triangles.


Instruction:
Can you tell what is the following Python 3 function doing
def capture_stdout():
    stdout = sys.stdout
    sys.stdout = six.moves.cStringIO()
    try:
        yield sys.stdout
    finally:
        sys.stdout = stdout

Response: Intercept standard output in a with - context


Instruction:
How would you implement a function in Python 3 that
changes current working directory in a with-context :param mkdir: If True, then directory is created if it does not exist :param cleanup: If True and no pre-existing directory, the directory is cleaned up at the end

Response:
def pushd(path, mkdir=True, cleanup=False):
    """Change current working directory in a with-context
    :param mkdir: If True, then directory is created if it does not exist
    :param cleanup: If True and no pre-existing directory, the directory is
    cleaned up at the end
    """
    cwd = os.getcwd()
    exists = osp.exists(path)
    if mkdir and not exists:
        os.makedirs(path)
    os.chdir(path)
    try:
        yield path
    finally:
        os.chdir(cwd)
        if not exists and cleanup:
            # NB: should we be checking for rmtree.avoids_symlink_attacks ?
            shutil.rmtree(path)


Instruction:
Create a Python 3 function to
create a temporary directory in a with - context keyword arguments.

Response:
def mkdtemp(*args, **kwargs):
    """Create a temporary directory in a with-context

    keyword remove: Remove the directory when leaving the
    context if True. Default is True.
    other keywords arguments are given to the tempfile.mkdtemp
    function.
    """
    remove = kwargs.pop('remove', True)
    path = tempfile.mkdtemp(*args, **kwargs)
    try:
        yield path
    finally:
        if remove:
            shutil.rmtree(path)


Instruction:
How would you explain what the following Python 3 function does
def elapsed(self):
        if self.end is None:
            return self() - self.end
        return self.end - self.start

Response: Returns the amount of time spent in the context.


Instruction:
Implement a function in Python 3 to
return a slug of the given string.

Response:
def slugify(value):
    """
    Normalizes string, converts to lowercase, removes non-alpha characters,
    and converts spaces to hyphens.type(
    """
    import re
    import unicodedata
    value = str(value)
    value = unicodedata.normalize('NFKD', value).encode('ascii', 'ignore').decode('utf8').strip().lower()
    value = re.sub(r'[^\w\s\-\.]', '', value)
    value = re.sub(r'[-\s]+', '-', value)
    return value


Instruction:
Make a summary of the following Python 3 code
def import_name_or_class(name):
    " Import an obect as either a fully qualified, dotted name, "

    if isinstance(name, str):

        # for "a.b.c.d" -> [ 'a.b.c', 'd' ]
        module_name, object_name = name.rsplit('.',1)
        # __import__ loads the multi-level of module, but returns
        # the top level, which we have to descend into
        mod = __import__(module_name)

        components = name.split('.')

        for comp in components[1:]: # Already got the top level, so start at 1

            mod = getattr(mod, comp)
        return mod
    else:
        return name

Response: Import an obect as either a fully qualified dotted name


Instruction:
Make a summary of the following Python 3 code
def pip_installer_url(version=None):
    version = version or hpcbench.__version__
    version = str(version)
    if '.dev' in version:
        git_rev = 'master'
        if 'TRAVIS_BRANCH' in os.environ:
            git_rev = version.split('+', 1)[-1]
            if '.' in git_rev:  # get rid of date suffix
                git_rev = git_rev.split('.', 1)[0]
            git_rev = git_rev[1:]  # get rid of scm letter
        return 'git+{project_url}@{git_rev}#egg=hpcbench'.format(
            project_url='http://github.com/BlueBrain/hpcbench',
            git_rev=git_rev or 'master',
        )
    return 'hpcbench=={}'.format(version)

Response: Return the URL to pip to install HPCBench.


Instruction:
Can you create a Python 3 function that
loads the campaign from YAML file

Response:
def from_file(campaign_file, **kwargs):
    """Load campaign from YAML file

    :return: memory representation of the YAML file
    :rtype: dictionary
    """
    realpath = osp.realpath(campaign_file)
    if osp.isdir(realpath):
        campaign_file = osp.join(campaign_file, YAML_CAMPAIGN_FILE)
    campaign = Configuration.from_file(campaign_file)
    return default_campaign(campaign, **kwargs)


Instruction:
Can you generate a brief explanation for the following Python 3 code
def default_campaign(
    campaign=None, expandcampvars=True, exclude_nodes=None, frozen=True
):
    """Fill an existing campaign with default
    values for optional keys

    :param campaign: dictionary
    :type campaign: str
    :param exclude_nodes: node set to exclude from allocations
    :type exclude_nodes: str
    :param expandcampvars: should env variables be expanded? True by default
    :type expandcampvars: bool
    :param frozen: whether the returned data-structure is immutable or not
    :type frozen: bool
    :return: object provided in parameter
    :rtype: dictionary
    """
    campaign = campaign or nameddict()

    def _merger(_camp, _deft):
        for key in _deft.keys():
            if (
                key in _camp
                and isinstance(_camp[key], dict)
                and isinstance(_deft[key], collections.Mapping)
            ):
                _merger(_camp[key], _deft[key])
            elif key not in _camp:
                _camp[key] = _deft[key]

    _merger(campaign, DEFAULT_CAMPAIGN)
    campaign.setdefault('campaign_id', str(uuid.uuid4()))

    for precondition in campaign.precondition.keys():
        config = campaign.precondition[precondition]
        if not isinstance(config, list):
            campaign.precondition[precondition] = [config]

    def _expandvars(value):
        if isinstance(value, six.string_types):
            return expandvars(value)
        return value

    if expandcampvars:
        campaign = nameddict(dict_map_kv(campaign, _expandvars))
    else:
        campaign = nameddict(campaign)
    if expandcampvars:
        if campaign.network.get('tags') is None:
            campaign.network['tags'] = {}
        NetworkConfig(campaign).expand()
    return freeze(campaign) if frozen else campaign

Response: Fill an existing campaign with default values for optional keys


Instruction:
Write a Python 3 function for
getting of benchmarks referenced in the configuration

Response:
def get_benchmark_types(campaign):
    """Get of benchmarks referenced in the configuration

    :return: benchmarks
    :rtype: string generator
    """
    for benchmarks in campaign.benchmarks.values():
        for name, benchmark in benchmarks.items():
            if name != 'sbatch':  # exclude special sbatch name
                yield benchmark.type


Instruction:
Explain what the following Python 3 code does
def get_metrics(campaign, report, top=True):
    if top and campaign.process.type == 'slurm':
        for path, _ in report.collect('jobid', with_path=True):
            for child in ReportNode(path).children.values():
                for metrics in get_metrics(campaign, child, top=False):
                    yield metrics
    else:

        def metrics_node_extract(report):
            metrics_file = osp.join(report.path, JSON_METRICS_FILE)
            if osp.exists(metrics_file):
                with open(metrics_file) as istr:
                    return json.load(istr)

        def metrics_iterator(report):
            return filter(
                lambda eax: eax[1] is not None,
                report.map(metrics_node_extract, with_path=True),
            )

        for path, metrics in metrics_iterator(report):
            yield report.path_context(path), metrics

Response: Extract metrics from existing campaign


Instruction:
Can you implement a function in Python 3 that
writes YAML campaign template to the given open file.

Response:
def write(self, file):
        """Write YAML campaign template to the given open file
        """
        render(
            self.template,
            file,
            benchmarks=self.benchmarks,
            hostname=socket.gethostname(),
        )


Instruction:
Given the following Python 3 function, write the documentation
def expand(self):
        if self.slurm:
            self._introspect_slurm_cluster()
        self.network.nodes = self._expand_nodes(self.network.nodes)
        self._expand_tags()

Response: Perform node expansion of network section.


Instruction:
Here you have a function in Python 3, explain what it does
def ensure_has_same_campaigns(self):
        lhs_yaml = osp.join(self.lhs, 'campaign.yaml')
        rhs_yaml = osp.join(self.rhs, 'campaign.yaml')
        assert osp.isfile(lhs_yaml)
        assert osp.isfile(rhs_yaml)
        assert filecmp.cmp(lhs_yaml, rhs_yaml)

Response: Ensure that the 2 campaigns to merge have been generated from the same campaign. yaml


Instruction:
Write a Python 3 function that can
build a dictionary of fields extracted from the given path

Response:
def path_context(self, path):
        """Build of dictionary of fields extracted from
        the given path"""
        prefix = os.path.commonprefix([path, self._path])
        relative_path = path[len(prefix) :]
        relative_path = relative_path.strip(os.sep)
        attrs = self.CONTEXT_ATTRS
        for i, elt in enumerate(relative_path.split(os.sep)):
            yield attrs[i], elt
        yield 'path', path


Instruction:
Can you generate a brief explanation for the following Python 3 code
def children(self):
        for child in self.data.get('children', []):
            if osp.exists(osp.join(self.path, child, YAML_REPORT_FILE)):
                yield child, self.__class__(osp.join(self.path, child))

Response:
get children node referenced as children in the
        report.


Instruction:
Can you generate the documentation for the following Python 3 function
def collect(self, *keys, **kwargs):
        if not keys:
            raise Exception('Missing key')
        has_values = functools.reduce(
            operator.__and__, [key in self.data for key in keys], True
        )
        if has_values:
            values = tuple([self.data[key] for key in keys])
            if len(values) == 1:
                values = values[0]
            if kwargs.get('with_path', False):
                yield self.path, values
            else:
                yield values
        if kwargs.get('recursive', True):
            for child in self.children.values():
                for value in child.collect(*keys, **kwargs):
                    yield value

Response: Generator function traversing the tree structure to collect values of a specified key.


Instruction:
Can you generate the documentation for the following Python 3 function
def collect_one(self, *args, **kwargs):
        generator = self.collect(*args, **kwargs)
        try:
            value = next(generator)
        except StopIteration:
            raise Exception("Expected exactly one value don't have any")
        try:
            next(generator)
        except StopIteration:
            return value
        raise Exception('Expected exactly one value but have more')

Response: Same as collect but expects to have only one result.


Instruction:
Write a Python 3 script to
set properties of the beams in the structure.

Response:
def set_beam_prop(self, prop, values, repeat="up"):
        """
        Specify the properties of the beams

        :param values:
        :param repeat: if 'up' then duplicate up the structure
        :return:
        """
        values = np.array(values)
        if repeat == "up":
            assert len(values.shape) == 1
            values = [values for ss in range(self.n_storeys)]
        else:
            assert len(values.shape) == 2
        if len(values[0]) != self.n_bays:
            raise ModelError("beam depths does not match number of bays (%i)." % self.n_bays)
        for ss in range(self.n_storeys):
            for i in range(self.n_bays):
                self._beams[ss][i].set_section_prop(prop, values[0][i])


Instruction:
Can you generate a brief explanation for the following Python 3 code
def set_column_prop(self, prop, values, repeat="up"):
        values = np.array(values)
        if repeat == "up":
            assert len(values.shape) == 1
            values = [values for ss in range(self.n_storeys)]
        else:
            assert len(values.shape) == 2
        if len(values[0]) != self.n_cols:
            raise ModelError("column props does not match n_cols (%i)." % self.n_cols)
        for ss in range(self.n_storeys):
            for i in range(self.n_cols):
                self._columns[ss][i].set_section_prop(prop, values[0][i])

Response: Set the properties of the columns in the structure.


Instruction:
Write a Python 3 script to
load the config file.

Response:
def load(self, path):
        """
        Load yaml-formatted config file.

        Parameters
        ----------
        path : str
            path to config file
        """
        with open(path) as f:
            self.config = full_load(f)
            if self.config is None:
                sys.stderr.write("Warning: config file is empty!\n")
                self.config = {}


Instruction:
Create a Python 3 function for
saving current state of config dictionary.

Response:
def save(self):
        """
        Save current state of config dictionary.
        """
        with open(self.config_file, "w") as f:
            f.write(dump(self.config, default_flow_style=False))


Instruction:
Write a Python 3 function for
registering an extension into the Nautilus Router

Response:
def register(self, extension, extension_name):
        """ Register an extension into the Nautilus Router

        :param extension: Extension
        :param extension_name: Name of the Extension
        :return:
        """
        self._extensions[extension_name] = extension
        self.ROUTES.extend([
            tuple(list(t) + [extension_name])
            for t in extension.ROUTES
        ])
        self.CACHED.extend([
            (f_name, extension_name)
            for f_name in extension.CACHED
        ])

        # This order allows for user defaults to overwrite extension ones
        self.Access_Control_Allow_Methods.update({
            k: v
            for k, v in extension.Access_Control_Allow_Methods.items()
            if k not in self.Access_Control_Allow_Methods
        })


Instruction:
Make a summary of the following Python 3 code
def setLogger(self, logger):
        self.logger = logger
        if logger is None:
            self.logger = logging.getLogger("capitains_nautilus")
            formatter = logging.Formatter("[%(asctime)s] {%(pathname)s:%(lineno)d} %(levelname)s - %(message)s")
            stream = FlaskNautilus.LoggingHandler()
            stream.setLevel(logging.INFO)
            stream.setFormatter(formatter)
            self.logger.addHandler(stream)

        if self.resolver:
            self.resolver.logger = self.logger

        return self.logger

Response: Set up the Logger for the application.


Instruction:
Make a summary of the following Python 3 code
def init_app(self, app):

        self.init_blueprint(app)

        if self.flaskcache is not None:
            for func, extension_name in self.CACHED:
                func = getattr(self._extensions[extension_name], func)
                setattr(
                    self._extensions[extension_name],
                    func.__name__,
                    self.flaskcache.memoize()(func)
                )

        return self.blueprint

Response:
Initiate the extension on the application and return the blueprint for the application that has been registered in app
       .


Instruction:
Can you generate a brief explanation for the following Python 3 code
def init_blueprint(self, app):
        self.blueprint = Blueprint(
            self.name,
            self.name,
            template_folder=resource_filename("capitains_nautilus", "data/templates"),
            url_prefix=self.prefix
        )

        # Register routes
        for url, name, methods, extension_name in self.ROUTES:
            self.blueprint.add_url_rule(
                url,
                view_func=self.view(name, extension_name),
                endpoint=name[2:],
                methods=methods
            )

        app.register_blueprint(self.blueprint)
        return self.blueprint

Response: This method creates the blueprint for the extension and registers routes and filters and connecting the app and the blueprints of the extension.


Instruction:
Write a Python 3 function that can
build a response according to a function name and extension name.

Response:
def view(self, function_name, extension_name):
        """ Builds response according to a function name

        :param function_name: Route name / function name
        :param extension_name: Name of the extension holding the function
        :return: Function
        """
        if isinstance(self.Access_Control_Allow_Origin, dict):
            d = {
                "Access-Control-Allow-Origin": self.Access_Control_Allow_Origin[function_name],
                "Access-Control-Allow-Methods": self.Access_Control_Allow_Methods[function_name]
            }
        else:
            d = {
                "Access-Control-Allow-Origin": self.Access_Control_Allow_Origin,
                "Access-Control-Allow-Methods": self.Access_Control_Allow_Methods[function_name]
            }

        def r(*x, **y):
            val = getattr(self._extensions[extension_name], function_name)(*x, **y)
            if isinstance(val, Response):
                val.headers.extend(d)
                return val
            else:
                val = list(val)
                val[2].update(d)
                return tuple(val)
        return r


Instruction:
Implement a Python 3 function for
preparing a slurm script that executes the experiment for a given project.

Response:
def script(experiment, projects):
    """
    Prepare a slurm script that executes the experiment for a given project.

    Args:
        experiment: The experiment we want to execute
        projects: All projects we generate an array job for.
    """
    benchbuild_c = local[local.path(sys.argv[0])]
    slurm_script = local.cwd / experiment.name + "-" + str(
        CFG['slurm']['script'])

    srun = local["srun"]
    srun_args = []
    if not CFG["slurm"]["multithread"]:
        srun_args.append("--hint=nomultithread")
    if not CFG["slurm"]["turbo"]:
        srun_args.append("--pstate-turbo=off")

    srun = srun[srun_args]
    srun = srun[benchbuild_c["run"]]

    return __save__(slurm_script, srun, experiment, projects)


Instruction:
Implement a Python 3 function for
fetching the text structure of a given work from a CTS endpoint.

Response:
def fetch_text_structure(urn, endpoint="http://cts.perseids.org/api/cts"):
    """
    Fetches the text structure of a given work from a CTS endpoint.

    :param urn: the work's CTS URN (at the work-level!,
        e.g."urn:cts:greekLit:tlg0012.tlg001")
    :type urn: string
    :param endpoint: the URL of the CTS endpoint to use (defaults to Perseids')
    :type endpoint: string
    :return: a dict with keys "urn", "provenance", "valid_reffs", "levels"
    :rtype: dict
    """

    structure = {
        "urn": urn,
        "provenance": endpoint,
        "valid_reffs": {}
    }

    orig_edition = None
    suffix = 'grc' if 'greekLit' in urn else 'lat'
    resolver = HttpCtsResolver(HttpCtsRetriever(endpoint))
    work_metadata = resolver.getMetadata(urn)

    # among all editions for this work, pick the one in Greek or Latin
    try:
        orig_edition = next(iter(
            work_metadata.children[edition]
            for edition in work_metadata.children
            if suffix in str(work_metadata.children[edition].urn)
        ))
    except Exception as e:
        print(e)
        return None

    assert orig_edition is not None

    # get information about the work's citation scheme
    structure["levels"] = [
        (n + 1, level.name.lower())
        for n, level in enumerate(orig_edition.citation)
    ]

    # for each hierarchical level of the text
    # fetch all citable text elements
    for level_n, level_label in structure["levels"]:

        structure["valid_reffs"][level_n] = []
        for ref in resolver.getReffs(urn, level=level_n):
            print(ref)
            element = {
                "current": "{}:{}".format(urn, ref),
            }
            if "." in ref:
                element["parent"] = "{}:{}".format(
                    urn,
                    ".".join(ref.split(".")[:level_n - 1])
                )
            # TODO: parallelize this bit
            textual_node = resolver.getTextualNode(
                textId=urn,
                subreference=ref,
                prevnext=True
            )
            if textual_node.nextId is not None:
                element["previous"] = "{}:{}".format(urn, textual_node.nextId)
            if textual_node.prevId is not None:
                element["following"] = "{}:{}".format(urn, textual_node.prevId)
            structure["valid_reffs"][level_n].append(element)

    return structure


Instruction:
Implement a Python 3 function for
executing all actions but don t do anything as extension.

Response:
def actions_for_project(self, project):
        """Execute all actions but don't do anything as extension."""
        project.runtime_extension = run.RuntimeExtension(project, self)
        return self.default_runtime_actions(project)


Instruction:
How would you explain what the following Python 3 function does
def install_uchroot(_):
    builddir = local.path(str(CFG["build_dir"].value))
    with local.cwd(builddir):
        erlent_src = local.path('erlent')
        erlent_git = erlent_src / '.git'
        erlent_repo = str(CFG['uchroot']['repo'])
        erlent_build = erlent_src / 'build'
        if not erlent_git.exists():
            git("clone", erlent_repo)
        else:
            with local.cwd(erlent_src):
                git("pull", "--rebase")

        erlent_build.mkdir()
        with local.cwd(erlent_build):
            cmake("../")
            make()

    os.environ["PATH"] = os.path.pathsep.join(
        [erlent_build, os.environ["PATH"]])
    local.env.update(PATH=os.environ["PATH"])

    if not find_package("uchroot"):
        LOG.error('uchroot not found, after updating PATH to %s',
                  os.environ['PATH'])
        sys.exit(-1)

    env = CFG['env'].value
    if 'PATH' not in env:
        env['PATH'] = []
    env['PATH'].append(str(erlent_build))

Response: Installs erlent and erlent build.


Instruction:
Write a Python 3 script to
print a small summary of the executed actions.

Response:
def print_summary(num_actions, failed, duration):
    """
    Print a small summary of the executed plan.

    Args:
        num_actions (int): Total size of the executed plan.
        failed (:obj:`list` of :obj:`actions.Step`): List of failed actions.
        duration: Time we spent executing the plan.
    """
    num_failed = len(failed)
    print("""
Summary:
{num_total} actions were in the queue.
{num_failed} actions failed to execute.

This run took: {elapsed_time:8.3f} seconds.
    """.format(
        num_total=num_actions, num_failed=num_failed, elapsed_time=duration))


Instruction:
Given the following Python 3 function, write the documentation
def read_json_sensor_file(ffp):
    sensor_path = ffp
    si = json.load(open(sensor_path))
    for m_type in si:
        # Convert keys from strings to integers
        si[m_type] = {int(k): v for k, v in si[m_type].items()}
    return si

Response: Reads the sensor file and stores it as a dictionary.


Instruction:
Write a Python 3 script to
get all sensor codes that match a wild sensor code

Response:
def get_all_sensor_codes(si, wild_sensor_code):
    """
    Get all sensor sensor_codes that match a wild sensor code

    :param si: dict, sensor index json dictionary
    :param wild_sensor_code: str, a sensor code with "*" for wildcards (e.g. ACCX-*-L2C-*)
    :return:
    """
    mtype_and_ory, x, y, z = wild_sensor_code.split("-")
    if mtype_and_ory == "*":
        mtypes = list(si)
    elif mtype_and_ory[-1] in "XYZ" and "ACCX" not in si:  # Need to support old sensor_file.json files.
        mtypes = [mtype_and_ory[:-1]]
    else:
        mtypes = [mtype_and_ory]

    all_sensor_codes = []
    for mtype in mtypes:
        for m_number in si[mtype]:
            if x in ["*", si[mtype][m_number]['X-CODE']] and \
                    y in ["*", si[mtype][m_number]['Y-CODE']] and \
                    z in ["*", si[mtype][m_number]['Z-CODE']]:
                cc = get_sensor_code_by_number(si, mtype, m_number)
                all_sensor_codes.append(cc)

    return all_sensor_codes


Instruction:
Explain what the following Python 3 code does
def get_sensor_code_by_number(si, mtype, sensor_number, quiet=False):
    try:
        if 'Orientation' in si[mtype][sensor_number]:
            orientation = si[mtype][sensor_number]['Orientation']
        else:
            orientation = ""
        return "%s%s-%s-%s-%s" % (mtype,
                                orientation,
                                si[mtype][sensor_number]['X-CODE'],
                                si[mtype][sensor_number]['Y-CODE'],
                                si[mtype][sensor_number]['Z-CODE'])
    except KeyError:
        if quiet:
            return None
        raise

Response: Given a sensor number get the full sensor code


Instruction:
How would you explain what the following Python 3 function does
def get_mtype_and_number_from_code(si, sensor_code):
    mtype_and_ory, x, y, z = sensor_code.split("-")
    if mtype_and_ory[-1] in "XYZ" and "ACCX" not in si:  # Need to support old sensor_file.json files.
        mtype = mtype_and_ory[:-1]
    else:
        mtype = mtype_and_ory
    for m_number in si[mtype]:
        cc = get_sensor_code_by_number(si, mtype, m_number)
        if cc == sensor_code:
            return mtype, m_number
    return None, None

Response: Given a sensor code get the motion type and sensor number from the sensor index json dictionary.


Instruction:
How would you explain what the following Python 3 function does
def PCExtension(*args, **kwargs):
    vals = {}

    intercept = {
        'build_callbacks': (),  # tuple of (callback, args, kwargs)
        'link_ext': True,
        'build_files': (),
        'dist_files': (),  # work around stackoverflow.com/questions/2994396/
        'template_regexps': [],
        'pass_extra_compile_args': False,  # use distutils extra_compile_args?
        'pycompilation_compile_kwargs': {},
        'pycompilation_link_kwargs': {},
    }
    for k, v in intercept.items():
        vals[k] = kwargs.pop(k, v)

    intercept2 = {
        'logger': None,
        'only_update': True,
    }
    for k, v in intercept2.items():
        vck = kwargs.pop(k, v)
        vck = vals['pycompilation_compile_kwargs'].pop(k, vck)
        vck = vck or vals['pycompilation_link_kwargs'].pop(k, vck)
        vals[k] = vck

    instance = Extension(*args, **kwargs)

    if vals['logger'] is True:
        # interpret as we should instantiate a logger
        import logging
        logging.basicConfig(level=logging.DEBUG)
        vals['logger'] = logging.getLogger('PCExtension')

    for k, v in vals.items():
        setattr(instance, k, v)

    return instance

Response: Returns a new object of type Extension with the given arguments and keyword arguments.


Instruction:
How would you implement a function in Python 3 that
copies or render a source file to output_dir.

Response:
def _copy_or_render_source(ext, f, output_dir, render_callback,
                           skip_copy=False):
    """
    Tries to do regex match for each (pattern, target, subsd) tuple
    in ext.template_regexps for file f.
    """
    # Either render a template or copy the source
    dirname = os.path.dirname(f)
    filename = os.path.basename(f)
    for pattern, target, subsd in ext.template_regexps:
        if re.match(pattern, filename):
            tgt = os.path.join(dirname, re.sub(
                pattern, target, filename))
            rw = MetaReaderWriter('.metadata_subsd')
            try:
                prev_subsd = rw.get_from_metadata_file(output_dir, f)
            except (FileNotFoundError, KeyError):
                prev_subsd = None

            render_callback(
                get_abspath(f),
                os.path.join(output_dir, tgt),
                subsd,
                only_update=ext.only_update,
                prev_subsd=prev_subsd,
                create_dest_dirs=True,
                logger=ext.logger)
            rw.save_to_metadata_file(output_dir, f, subsd)
            return tgt
    else:
        if not skip_copy:
            copy(f,
                 os.path.join(output_dir,
                              os.path.dirname(f)),
                 only_update=ext.only_update,
                 dest_is_dir=True,
                 create_dest_dirs=True,
                 logger=ext.logger)
        return f


Instruction:
Explain what the following Python 3 code does
def render_python_template_to(src, dest, subsd, only_update=False,
                              prev_subsd=None, create_dest_dirs=True,
                              logger=None):
        """
        Overload this function if you want to use a template engine such as
        e.g. mako.
        """
        if only_update:
            if subsd == prev_subsd:
                if not missing_or_other_newer(dest, src):
                    if logger:
                        msg = ("Did not re-render {}. "
                               "(destination newer + same dict)")
                        logger.info(msg.format(src))
                    return

        with open(src, 'rt') as ifh:
            data = ifh.read()  # Don't go crazy on file size...

        if create_dest_dirs:
            dest_dir = os.path.dirname(dest)
            if not os.path.exists(dest_dir):
                make_dirs(dest_dir)

        with open(dest, 'wt') as ofh:
            ofh.write(data % subsd)

Response: Render a python template file to a new location.


Instruction:
Create a Python 3 function for
returning a list of names in that language.

Response:
def get_names(self):
        """
        Returns a dict where key is the language and value is the name in that language.

        Example:
            {'it':"Sofocle"}
        """
        names = [id for id in self.ecrm_P1_is_identified_by if id.uri == surf.ns.EFRBROO['F12_Name']]
        self.names = []
        for name in names:
            for variant in name.rdfs_label:
                self.names.append((variant.language,variant.title()))
        return self.names


Instruction:
Here you have a function in Python 3, explain what it does
def add_name(self, name, lang=None):
        try:
            assert (lang, name) not in self.get_names()
        except Exception as e:
            # TODO: raise a custom exception
            logger.warning("Duplicate name detected while adding \"%s (lang=%s)\""%(name, lang))
            return False
        newlabel = Literal(name, lang=lang) if lang is not None else \
            Literal(name)
        name = [
            id
            for id in self.ecrm_P1_is_identified_by
            if id.uri == surf.ns.EFRBROO['F12_Name']
        ][0]
        try:
            name.rdfs_label.append(newlabel)
            name.update()
            return True
        except Exception as e:
            raise e

Response: Adds a new name variant to an author.


Instruction:
How would you explain what the following Python 3 function does
def add_abbreviation(self, new_abbreviation):
        try:
            assert new_abbreviation not in self.get_abbreviations()
        except Exception as e:
            # TODO: raise a custom exception
            logger.warning("Duplicate abbreviation detected while adding \"%s\""%new_abbreviation)
            return False

        try:
            type_abbreviation = self.session.get_resource(BASE_URI_TYPES % "abbreviation"
                                                        , self.session.get_class(surf.ns.ECRM['E55_Type']))
            abbreviation = [abbreviation
                                for name in self.ecrm_P1_is_identified_by
                                    for abbreviation in name.ecrm_P139_has_alternative_form
                                        if name.uri == surf.ns.EFRBROO['F12_Name']
                                            and abbreviation.ecrm_P2_has_type.first == type_abbreviation][0]
            abbreviation.rdfs_label.append(Literal(new_abbreviation))
            abbreviation.update()
            return True
        except IndexError as e:
            # means there is no abbreviation instance yet
            type_abbreviation = self.session.get_resource(BASE_URI_TYPES % "abbreviation"
                                                        , self.session.get_class(surf.ns.ECRM['E55_Type']))
            Appellation = self.session.get_class(surf.ns.ECRM['E41_Appellation'])
            abbreviation_uri = "%s/abbr" % str(self.subject)
            abbreviation = Appellation(abbreviation_uri)
            abbreviation.ecrm_P2_has_type = type_abbreviation
            abbreviation.rdfs_label.append(Literal(new_abbreviation))
            abbreviation.save()

            name = (name for name in self.ecrm_P1_is_identified_by if name.uri == surf.ns.EFRBROO['F12_Name']).next()
            name.ecrm_P139_has_alternative_form = abbreviation
            name.update()
            return True
        except Exception as e:
            raise e

Response: Adds a new name variant to an author.


Instruction:
Write a Python 3 script for
getting abbreviations of the names of the author.

Response:
def get_abbreviations(self):
        """
        Get abbreviations of the names of the author.

        :return: a list of strings (empty list if no abbreviations available).
        """
        abbreviations = []
        try:
            type_abbreviation = self.session.get_resource(BASE_URI_TYPES % "abbreviation"
                                                        , self.session.get_class(surf.ns.ECRM['E55_Type']))
            abbreviations = [unicode(label)
                                for name in self.ecrm_P1_is_identified_by
                                    for abbreviation in name.ecrm_P139_has_alternative_form
                                        for label in abbreviation.rdfs_label
                                            if name.uri == surf.ns.EFRBROO['F12_Name']
                                                and abbreviation.ecrm_P2_has_type.first == type_abbreviation]
        except Exception as e:
            logger.debug("Exception raised when getting abbreviations for %a"%self)
        finally:
            return abbreviations


Instruction:
How would you explain what the following Python 3 function does
def get_urn(self):
        # TODO: check type
        try:
            type_ctsurn = self.session.get_resource(BASE_URI_TYPES % "CTS_URN"
                                                    , self.session.get_class(surf.ns.ECRM['E55_Type']))
            urn = [CTS_URN(urnstring.rdfs_label.one)
                                for urnstring in self.ecrm_P1_is_identified_by
                                                            if urnstring.uri == surf.ns.ECRM['E42_Identifier']
                                                            and urnstring.ecrm_P2_has_type.first == type_ctsurn][0]
            return urn
        except Exception as e:
            return None

Response: Returns the Urn of the HucitAuthor s CTS.


Instruction:
Write a Python 3 script for
changing the CTS URN of the author or adds a new one.

Response:
def set_urn(self,urn):
        """
        Change the CTS URN of the author or adds a new one (if no URN is assigned).
        """
        Type = self.session.get_class(surf.ns.ECRM['E55_Type'])
        Identifier = self.session.get_class(surf.ns.ECRM['E42_Identifier'])
        id_uri = "%s/cts_urn"%str(self.subject)
        try:
            id = Identifier(id_uri)
            id.rdfs_label = Literal(urn)
            id.ecrm_P2_has_type = Type(BASE_URI_TYPES % "CTS_URN")
            id.save()
            return True
        except Exception, e:
            raise e


Instruction:
How would you explain what the following Python 3 function does
def to_json(self):
        names = self.get_names()
        return json.dumps({
                    "uri" : self.subject
                    , "urn" : str(self.get_urn())
                    , "names" : [{"language":lang, "label":label} for lang, label in names]
                    , "name_abbreviations" : self.get_abbreviations()
                    , "works" : [json.loads(work.to_json()) for work in self.get_works()]
                }, indent=2)

Response: Serialises a HucitAuthor to a JSON formatted string.


Instruction:
Write a Python 3 function that can
return a list of tuples containing language and title.

Response:
def get_titles(self):
        """TODO"""
        return [(label.language, unicode(label)) for label in self.efrbroo_P102_has_title.first.rdfs_label]


Instruction:
How would you explain what the following Python 3 function does
def get_abbreviations(self, combine=False):
        abbreviations = []
        try:
            type_abbreviation = self.session.get_resource(BASE_URI_TYPES % "abbreviation"
                                                        , self.session.get_class(surf.ns.ECRM['E55_Type']))
            abbreviations = [unicode(label)
                                for title in self.efrbroo_P102_has_title
                                    for abbreviation in title.ecrm_P139_has_alternative_form
                                        for label in abbreviation.rdfs_label
                                            if title.uri == surf.ns.EFRBROO['E35_Title']
                                             and abbreviation.ecrm_P2_has_type.first == type_abbreviation]

            if (combine and len(abbreviations)>0 and len(self.author.get_abbreviations())>=1):
                abbreviations = ["%s %s"%(author_abbrev, work_abbrev)
                                                for author_abbrev, work_abbrev in itertools.product(self.author.get_abbreviations()
                                                                                , self.get_abbreviations())]
        except Exception as e:
            logger.debug("Exception raised when getting abbreviations for %a"%self)
        finally:
            return abbreviations

Response: Get the abbreviations of the titles of the work.


Instruction:
Implement a Python 3 function for
adding a citable text structure to the work.

Response:
def add_text_structure(self, label):
        """
        Adds a citable text structure to the work.
        """

        ts = self.session.get_resource("%s/text_structure" % self.subject
                                      , self.session.get_class(surf.ns.HUCIT['TextStructure']))
        ts.rdfs_label.append(Literal(label))
        ts.save()
        self.hucit_has_structure = ts
        self.update()
        return self.hucit_has_structure.one


Instruction:
Given the following Python 3 function, write the documentation
def remove_text_structure(self, text_structure): # TODO: delete also TextElements one by one
        idx = self.hucit_has_structure.index(text_structure)
        ts = self.hucit_has_structure.pop(idx)
        ts.remove()
        self.update()
        return

Response: Remove any citable text structure from the work.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def _get_opus_maximum(self):
        label = """The opux maximum of a given author
                    that is, the only preserved work by that
                    author or the most known one."""

        opmax = self.session.get_resource(
            BASE_URI_TYPES % "opmax",
            self.session.get_class(surf.ns.ECRM['E55_Type'])
        )
        if opmax.is_present():
            return opmax
        else:
            opmax.rdfs_label.append(Literal(label, "en"))
            logger.debug("Created a new opus maximum type instance")
            opmax.save()
            return opmax

Response: Instantiate an opus maximum type.


Instruction:
Here you have a function in Python 3, explain what it does
def set_as_opus_maximum(self):  # TODO: test
        if self.is_opus_maximum():
            return False
        else:
            opmax = self._get_opus_maximum()
            self.ecrm_P2_has_type = opmax
            self.update()
            return True

Response: Mark the work as the author s opus maximum.


Instruction:
Write a Python 3 function for
checking whether the work is the author s opus maximum.

Response:
def is_opus_maximum(self):
        """Check whether the work is the author's opus maximum.

        Two cases:
        1. the work is flagged as opus max
        2. there is only one work by this author

        :return: boolean
        """
        opmax = self._get_opus_maximum()
        types = self.ecrm_P2_has_type

        if opmax in types:
            return True
        else:
            if len(self.author.get_works()) == 1:
                return True
            else:
                return False


Instruction:
Write a Python 3 script to
return the author to whom the work is attributed.

Response:
def author(self):
        """
        Returns the author to whom the work is attributed.

        :return: an instance of `HucitWork` # TODO: check that's the case
        """
        CreationEvent = self.session.get_class(surf.ns.EFRBROO['F27_Work_Conception'])
        Person = self.session.get_class(surf.ns.EFRBROO['F10_Person'])
        creation_event =  CreationEvent.get_by(efrbroo_R16_initiated=self).first()
        return Person.get_by(efrbroo_P14i_performed = creation_event).first()


Instruction:
Can you write a function in Python 3 where it
serialises a HucitWork to a JSON formatted string.

Response:
def to_json(self):
        """
        Serialises a HucitWork to a JSON formatted string.
        """
        titles = self.get_titles()
        return json.dumps({
                "uri" : self.subject
                , "urn" : str(self.get_urn())
                , "titles" : [{"language":lang, "label":label} for lang, label in titles]
                , "title_abbreviations" : self.get_abbreviations()

            }, indent=2)


Instruction:
Write a Python 3 script for
getting the ULID of the one - item item.

Response:
def get_urn(self):
        """
        TODO
        """
        urn = self.ecrm_P1_is_identified_by.one
        try:
            return CTS_URN(urn)
        except Exception, e:
            raise e


Instruction:
Here you have a function in Python 3, explain what it does
def nodes(self, tag):
        if tag == '*':
            return sorted(list(set(self.campaign.network.nodes)))
        definitions = self.campaign.network.tags.get(tag)
        if definitions is None:
            return []
        nodes = set()
        for definition in definitions:
            if len(definition.items()) == 0:
                continue
            mode, value = list(definition.items())[0]
            if mode == 'match':
                nodes = nodes.union(
                    set(
                        [
                            node
                            for node in self.campaign.network.nodes
                            if value.match(node)
                        ]
                    )
                )
            elif mode == 'constraint':
                slurm_nodes = os.environ.get('SLURM_JOB_NODELIST')
                if slurm_nodes:
                    nodes = NodeSet(slurm_nodes)
                else:
                    return ConstraintTag(tag, value)
            else:
                assert mode == 'nodes'
                nodes = nodes.union(set(value))
        return sorted(list(nodes))

Response: get nodes that belong to a tag


Instruction:
Can you tell what is the following Python 3 function doing
def execution_cls(self):
        name = self.campaign.process.type
        for clazz in [ExecutionDriver, SrunExecutionDriver]:
            if name == clazz.name:
                return clazz
        raise NameError("Unknown execution layer: '%s'" % name)

Response: Get execution layer class


Instruction:
Can you tell what is the following Python 3 function doing
def children(self):
        tags = {'*'}
        if self.tag:
            network_tags = {self.tag: self.campaign.network.tags[self.tag]}
        else:
            network_tags = self.campaign.network.tags
        for tag, configs in network_tags.items():
            for config in configs:
                for mode, kconfig in config.items():
                    if mode == 'match':
                        if kconfig.match(self.name) or kconfig.match(LOCALHOST):
                            tags.add(tag)
                            break
                    elif mode == 'nodes':
                        if self.name in kconfig or LOCALHOST in kconfig:
                            tags.add(tag)
                            break
                    elif mode == 'constraint':
                        tags.add(tag)
                        break
                if tag in tags:
                    break
        return tags

Response: Retrieve tags associated to the current node


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

        if value is not None:
            assert type(value) is bool, "'{0}' attribute: '{1}' type is not 'bool'!".format("activated", value)
        self.__activated = value

Response: Sets the activated flag.


Instruction:
Write a Python 3 function for
setting the value of the isSetted attribute.

Response:
def initialized(self, value):
        """
        Setter for **self.__initialized** attribute.

        :param value: Attribute value.
        :type value: bool
        """

        if value is not None:
            assert type(value) is bool, "'{0}' attribute: '{1}' type is not 'bool'!".format("initialized", value)
        self.__initialized = value


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

        raise NotImplementedError("{0} | '{1}' must be implemented by '{2}' subclasses!".format(
            self.__class__.__name__, self.activate.__name__, self.__class__.__name__))

Response: Sets Component activation state.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def reservations(self):
        command = [SINFO, '--reservation']
        output = subprocess.check_output(command, env=SINFO_ENV)
        output = output.decode()
        it = iter(output.splitlines())
        next(it)
        for line in it:
            rsv = Reservation.from_sinfo(line)
            yield rsv.name, rsv

Response: get nodes of every reservations


Instruction:
Given the following Python 3 function, write the documentation
def set(self, values):
        if hasattr(self, "inputs"):
            for item in self.inputs:
                if hasattr(self, item):
                    setattr(self, item, values[item])

Response: Set the object parameters using a dictionary


Instruction:
Write a Python 3 function for
executing the plan. Args: plan (:obj:`list` of :obj:`actions.Step`): The plan we want to execute. Returns: (:obj:`list` of :obj:`actions.Step`): A list of failed actions.

Response:
def execute_plan(plan):
    """"Execute the plan.

    Args:
        plan (:obj:`list` of :obj:`actions.Step`): The plan we want to execute.

    Returns:
        (:obj:`list` of :obj:`actions.Step`): A list of failed actions.
    """
    results = [action() for action in plan]
    return [result for result in results if actns.step_has_failed(result)]


Instruction:
Can you generate a brief explanation for the following Python 3 code
def _set_log_format(color, include_caller):
    level_name = '* %(levelname)1s'
    time = '%(asctime)s,%(msecs)03d'
    message = '%(message)s'
    color_start = '%(color)s'
    color_end = '%(end_color)s'
    caller = '[%(module)s:%(lineno)d]'

    if color:
        if include_caller:
            return '{}{}{}  {}  {} {}'.format(color_start, level_name, color_end, time, message, caller)
        else:
            return '{}{}{}  {}  {}'.format(color_start, level_name, color_end, time, message)
    else:
        if include_caller:
            return '{}  {}  {} {}'.format(level_name, time, message, caller)
        else:
            return '{}  {}  {}'.format(level_name, time, message)

Response:
Set log format
    :param color: Log message is colored
    :param include_caller: At the end, put a [caller:line-of-code], e.g. [script:123]
    :return: string of log format