Instruction:
Can you create a Python 3 function that
compiles the template string into a new base object.

Response:
def _CompileTemplate(
        template_str, builder, meta='{}', format_char='|', default_formatter='str',
        whitespace='smart'):
    """Compile the template string, calling methods on the 'program builder'.

  Args:
    template_str: The template string.  It should not have any compilation
        options in the header -- those are parsed by FromString/FromFile

    builder: The interface of _ProgramBuilder isn't fixed.  Use at your own
        risk.

    meta: The metacharacters to use, e.g. '{}', '[]'.

    default_formatter: The formatter to use for substitutions that are missing a
        formatter.  The 'str' formatter the "default default" -- it just tries
        to convert the context value to a string in some unspecified manner.

    whitespace: 'smart' or 'strip-line'.  In smart mode, if a directive is alone
        on a line, with only whitespace on either side, then the whitespace is
        removed.  In 'strip-line' mode, every line is stripped of its
        leading and trailing whitespace.

  Returns:
    The compiled program (obtained from the builder)

  Raises:
    The various subclasses of CompilationError.  For example, if
    default_formatter=None, and a variable is missing a formatter, then
    MissingFormatter is raised.

  This function is public so it can be used by other tools, e.g. a syntax
  checking tool run before submitting a template to source control.
  """
    meta_left, meta_right = SplitMeta(meta)

    # : is meant to look like Python 3000 formatting {foo:.3f}.  According to
    # PEP 3101, that's also what .NET uses.
    # | is more readable, but, more importantly, reminiscent of pipes, which is
    # useful for multiple formatters, e.g. {name|js-string|html}
    if format_char not in (':', '|'):
        raise ConfigurationError(
            'Only format characters : and | are accepted (got %r)' % format_char)

    if whitespace not in ('smart', 'strip-line'):
        raise ConfigurationError('Invalid whitespace mode %r' % whitespace)

    # If we go to -1, then we got too many {end}.  If end at 1, then we're missing
    # an {end}.
    balance_counter = 0
    comment_counter = 0  # ditto for ##BEGIN/##END

    has_defines = False

    for token_type, token in _Tokenize(template_str, meta_left, meta_right,
                                       whitespace):
        if token_type == COMMENT_BEGIN_TOKEN:
            comment_counter += 1
            continue
        if token_type == COMMENT_END_TOKEN:
            comment_counter -= 1
            if comment_counter < 0:
                raise CompilationError('Got too many ##END markers')
            continue
        # Don't process any tokens
        if comment_counter > 0:
            continue

        if token_type in (LITERAL_TOKEN, META_LITERAL_TOKEN):
            if token:
                builder.Append(token)
            continue

        if token_type in (SECTION_TOKEN, REPEATED_SECTION_TOKEN, DEF_TOKEN):
            parts = [p.strip() for p in token.split(format_char)]
            if len(parts) == 1:
                name = parts[0]
                formatters = []
            else:
                name = parts[0]
                formatters = parts[1:]
            builder.NewSection(token_type, name, formatters)
            balance_counter += 1
            if token_type == DEF_TOKEN:
                has_defines = True
            continue

        if token_type == PREDICATE_TOKEN:
            # {.attr?} lookups
            builder.NewPredicateSection(token, test_attr=True)
            balance_counter += 1
            continue

        if token_type == IF_TOKEN:
            builder.NewPredicateSection(token, test_attr=False)
            balance_counter += 1
            continue

        if token_type == OR_TOKEN:
            builder.NewOrClause(token)
            continue

        if token_type == ALTERNATES_TOKEN:
            builder.AlternatesWith()
            continue

        if token_type == END_TOKEN:
            balance_counter -= 1
            if balance_counter < 0:
                # TODO: Show some context for errors
                raise TemplateSyntaxError(
                    'Got too many %send%s statements.  You may have mistyped an '
                    "earlier 'section' or 'repeated section' directive."
                    % (meta_left, meta_right))
            builder.EndSection()
            continue

        if token_type == SUBST_TOKEN:
            parts = [p.strip() for p in token.split(format_char)]
            if len(parts) == 1:
                if default_formatter is None:
                    raise MissingFormatter('This template requires explicit formatters.')
                # If no formatter is specified, the default is the 'str' formatter,
                # which the user can define however they desire.
                name = token
                formatters = [default_formatter]
            else:
                name = parts[0]
                formatters = parts[1:]

            builder.AppendSubstitution(name, formatters)
            continue

        if token_type == SUBST_TEMPLATE_TOKEN:
            # no formatters
            builder.AppendTemplateSubstitution(token)
            continue

    if balance_counter != 0:
        raise TemplateSyntaxError('Got too few %send%s statements' %
                                  (meta_left, meta_right))
    if comment_counter != 0:
        raise CompilationError('Got %d more {##BEGIN}s than {##END}s' % comment_counter)

    return builder.Root(), has_defines


Instruction:
Can you write a function in Python 3 where it
likes FromFile but takes a string.

Response:
def FromString(s, **kwargs):
    """Like FromFile, but takes a string."""

    f = StringIO.StringIO(s)
    return FromFile(f, **kwargs)


Instruction:
Write a Python 3 script to
parse a template from a file.

Response:
def FromFile(f, more_formatters=lambda x: None, more_predicates=lambda x: None,
             _constructor=None):
    """Parse a template from a file, using a simple file format.

  This is useful when you want to include template options in a data file,
  rather than in the source code.

  The format is similar to HTTP or E-mail headers.  The first lines of the file
  can specify template options, such as the metacharacters to use.  One blank
  line must separate the options from the template body.

  Example:

    default-formatter: none
    meta: {{}}
    format-char: :
    <blank line required>
    Template goes here: {{variable:html}}

  Args:
    f: A file handle to read from.  Caller is responsible for opening and
    closing it.
  """
    _constructor = _constructor or Template

    options = {}

    # Parse lines until the first one that doesn't look like an option
    while 1:
        line = f.readline()
        match = _OPTION_RE.match(line)
        if match:
            name, value = match.group(1), match.group(2)

            # Accept something like 'Default-Formatter: raw'.  This syntax is like
            # HTTP/E-mail headers.
            name = name.lower()
            # In Python 2.4, kwargs must be plain strings
            name = name.encode('utf-8')

            if name in _OPTION_NAMES:
                name = name.replace('-', '_')
                value = value.strip()
                if name == 'default_formatter' and value.lower() == 'none':
                    value = None
                options[name] = value
            else:
                break
        else:
            break

    if options:
        if line.strip():
            raise CompilationError(
                'Must be one blank line between template options and body (got %r)'
                % line)
        body = f.read()
    else:
        # There were no options, so no blank line is necessary.
        body = line + f.read()

    return _constructor(body,
                        more_formatters=more_formatters,
                        more_predicates=more_predicates,
                        **options)


Instruction:
Can you tell what is the following Python 3 function doing
def _MakeGroupFromRootSection(root_section, undefined_str):
    group = {}
    for statement in root_section.Statements():
        if isinstance(statement, six.string_types):
            continue
        func, args = statement
        # here the function acts as ID for the block type
        if func is _DoDef and isinstance(args, _Section):
            section = args
            # Construct a Template instance from a this _Section subtree
            t = Template._FromSection(section, group, undefined_str)
            group[section.section_name] = t
    return group

Response: Construct a dictinary template name - > Template instance from a parse tree.


Instruction:
Given the following Python 3 function, write the documentation
def JoinTokens(tokens):
    try:
        return ''.join(tokens)
    except UnicodeDecodeError:
        # This can still raise UnicodeDecodeError if that data isn't utf-8.
        return ''.join(t.decode('utf-8') for t in tokens)

Response: Join tokens into a single string.


Instruction:
How would you implement a function in Python 3 that
executes the repeated section.

Response:
def _DoRepeatedSection(args, context, callback, trace):
    """{.repeated section foo}"""

    block = args

    items = context.PushSection(block.section_name, block.pre_formatters)
    if items:
        if not isinstance(items, list):
            raise EvaluationError('Expected a list; got %s' % type(items))

        last_index = len(items) - 1
        statements = block.Statements()
        alt_statements = block.Statements('alternates with')
        try:
            i = 0
            while True:
                context.Next()
                # Execute the statements in the block for every item in the list.
                # Execute the alternate block on every iteration except the last.  Each
                # item could be an atom (string, integer, etc.) or a dictionary.
                _Execute(statements, context, callback, trace)
                if i != last_index:
                    _Execute(alt_statements, context, callback, trace)
                i += 1
        except StopIteration:
            pass

    else:
        _Execute(block.Statements('or'), context, callback, trace)

    context.Pop()


Instruction:
Write a Python 3 script to
execute a section statement.

Response:
def _DoSection(args, context, callback, trace):
    """{.section foo}"""
    block = args
    # If a section present and "true", push the dictionary onto the stack as the
    # new context, and show it
    if context.PushSection(block.section_name, block.pre_formatters):
        _Execute(block.Statements(), context, callback, trace)
        context.Pop()
    else:  # missing or "false" -- show the {.or} section
        context.Pop()
        _Execute(block.Statements('or'), context, callback, trace)


Instruction:
Can you generate the documentation for the following Python 3 function
def _DoPredicates(args, context, callback, trace):
    block = args
    value = context.Lookup('@')
    for (predicate, args, func_type), statements in block.clauses:
        if func_type == ENHANCED_FUNC:
            do_clause = predicate(value, context, args)
        else:
            do_clause = predicate(value)

        if do_clause:
            if trace: trace.Push(predicate)
            _Execute(statements, context, callback, trace)
            if trace: trace.Pop()
            break

Response: Execute the predicate clauses.


Instruction:
Can you tell what is the following Python 3 function doing
def _Execute(statements, context, callback, trace):
    # Every time we call _Execute, increase this depth
    if trace:
        trace.exec_depth += 1
    for i, statement in enumerate(statements):
        if isinstance(statement, six.string_types):
            callback(statement)
        else:
            # In the case of a substitution, args is a pair (name, formatters).
            # In the case of a section, it's a _Section instance.
            try:
                func, args = statement
                func(args, context, callback, trace)
            except UndefinedVariable as e:
                # Show context for statements
                start = max(0, i - 3)
                end = i + 3
                e.near = statements[start:end]
                e.trace = trace  # Attach caller's trace (could be None)
                raise

Response: Execute a bunch of template statements in a ScopedContext.


Instruction:
How would you code a function in Python 3 to
free function to expand a template string with a data dictionary.

Response:
def expand(template_str, dictionary, **kwargs):
    """Free function to expands a template string with a data dictionary.

  This is useful for cases where you don't care about saving the result of
  compilation (similar to re.match('.*', s) vs DOT_STAR.match(s))
  """
    t = Template(template_str, **kwargs)
    return t.expand(dictionary)


Instruction:
Can you generate a brief explanation for the following Python 3 code
def _FlattenToCallback(tokens, callback):
    for t in tokens:
        if isinstance(t, six.string_types):
            callback(t)
        else:
            _FlattenToCallback(t, callback)

Response: Flatten a nested list structure into a callback function.


Instruction:
How would you explain what the following Python 3 function does
def execute_with_style_LEGACY(template, style, data, callback, body_subtree='body'):
    try:
        body_data = data[body_subtree]
    except KeyError:
        raise EvaluationError('Data dictionary has no subtree %r' % body_subtree)
    tokens_body = []
    template.execute(body_data, tokens_body.append)
    data[body_subtree] = tokens_body
    tokens = []
    style.execute(data, tokens.append)
    _FlattenToCallback(tokens, callback)

Response: Execute a template with a style.


Instruction:
Make a summary of the following Python 3 code
def expand_with_style(template, style, data, body_subtree='body'):
    if template.has_defines:
        return template.expand(data, style=style)
    else:
        tokens = []
        execute_with_style_LEGACY(template, style, data, tokens.append,
                                  body_subtree=body_subtree)
        return JoinTokens(tokens)

Response: Expand a data dictionary with a template AND a style.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def LookupWithType(self, user_str):
        prefix = 'template '
        ref = None  # fail the lookup by default
        if user_str.startswith(prefix):
            name = user_str[len(prefix):]
            if name == 'SELF':
                # we can resolve this right away
                ref = _TemplateRef(template=self.owner)  # special value
            else:
                ref = _TemplateRef(name)

        return ref, (), TEMPLATE_FORMATTER

Response:
Returns:
      ref: Either a template instance (itself) or _TemplateRef


Instruction:
Can you generate a brief explanation for the following Python 3 code
def _GetFormatter(self, format_str):
        formatter, args, func_type = self.formatters.LookupWithType(format_str)
        if formatter:
            return formatter, args, func_type
        else:
            raise BadFormatter('%r is not a valid formatter' % format_str)

Response: Get a formatter from a user s formatters list.


Instruction:
Given the following Python 3 function, write the documentation
def _GetPredicate(self, pred_str, test_attr=False):
        predicate, args, func_type = self.predicates.LookupWithType(pred_str)
        if predicate:
            pred = predicate, args, func_type
        else:
            # Nicer syntax, {.debug?} is shorthand for {.if test debug}.
            # Currently there is not if/elif chain; just use
            # {.if test debug} {.or test release} {.or} {.end}
            if test_attr:
                assert pred_str.endswith('?')
                # func, args, func_type
                pred = (_TestAttribute, (pred_str[:-1],), ENHANCED_FUNC)
            else:
                raise BadPredicate('%r is not a valid predicate' % pred_str)
        return pred

Response: Get predicate from a string.


Instruction:
Given the following Python 3 function, write the documentation
def NewSection(self, token_type, section_name, pre_formatters):
        pre_formatters = [self._GetFormatter(f) for f in pre_formatters]

        # TODO: Consider getting rid of this dispatching, and turn _Do* into methods
        if token_type == REPEATED_SECTION_TOKEN:
            new_block = _RepeatedSection(section_name, pre_formatters)
            func = _DoRepeatedSection
        elif token_type == SECTION_TOKEN:
            new_block = _Section(section_name, pre_formatters)
            func = _DoSection
        elif token_type == DEF_TOKEN:
            new_block = _Section(section_name, [])
            func = _DoDef
        else:
            raise AssertionError('Invalid token type %s' % token_type)

        self._NewSection(func, new_block)

Response: For sections or repeated sections.


Instruction:
How would you code a function in Python 3 to
add an or clause to the current section.

Response:
def NewOrClause(self, pred_str):
        """
    {.or ...} Can appear inside predicate blocks or section blocks, with
    slightly different meaning.
    """
        if pred_str:
            pred = self._GetPredicate(pred_str, test_attr=False)
        else:
            pred = None
        self.current_section.NewOrClause(pred)


Instruction:
Create a Python 3 function for
giving a section name push it on the top of the stack.

Response:
def PushSection(self, name, pre_formatters):
        """Given a section name, push it on the top of the stack.

    Returns:
      The new section, or None if there is no such section.
    """
        if name == '@':
            value = self.stack[-1].context
        else:
            value = self.stack[-1].context.get(name)

        # Apply pre-formatters
        for i, (f, args, formatter_type) in enumerate(pre_formatters):
            if formatter_type == ENHANCED_FUNC:
                value = f(value, self, args)
            elif formatter_type == SIMPLE_FUNC:
                value = f(value)
            else:
                assert False, 'Invalid formatter type %r' % formatter_type

        self.stack.append(_Frame(value))
        return value


Instruction:
Given the following Python 3 function, write the documentation
def Next(self):
        stacktop = self.stack[-1]

        # Now we're iterating -- push a new mutable object onto the stack
        if stacktop.index == -1:
            stacktop = _Frame(None, index=0)
            self.stack.append(stacktop)

        context_array = self.stack[-2].context

        if stacktop.index == len(context_array):
            self.stack.pop()
            raise StopIteration

        stacktop.context = context_array[stacktop.index]
        stacktop.index += 1

        return True

Response: Advance to the next item in a repeated section.


Instruction:
How would you implement a function in Python 3 that
looks up the stack for the given name.

Response:
def _LookUpStack(self, name):
        """Look up the stack for the given name."""
        i = len(self.stack) - 1
        while 1:
            frame = self.stack[i]
            if name == '@index':
                if frame.index != -1:  # -1 is undefined
                    return frame.index  # @index is 1-based
            else:
                context = frame.context
                if hasattr(context, 'get'):  # Can't look up names in a list or atom
                    try:
                        return context[name]
                    except KeyError:
                        pass

            i -= 1  # Next frame
            if i <= -1:  # Couldn't find it anywhere
                return self._Undefined(name)


Instruction:
Write a Python 3 script to
look up the value associated with a name in the current context.

Response:
def Lookup(self, name):
        """Get the value associated with a name in the current context.

    The current context could be an dictionary in a list, or a dictionary
    outside a list.

    Args:
      name: name to lookup, e.g. 'foo' or 'foo.bar.baz'

    Returns:
      The value, or self.undefined_str

    Raises:
      UndefinedVariable if self.undefined_str is not set
    """
        if name == '@':
            return self.stack[-1].context

        parts = name.split('.')
        value = self._LookUpStack(parts[0])

        # Now do simple lookups of the rest of the parts
        for part in parts[1:]:
            try:
                value = value[part]
            except (KeyError, TypeError):  # TypeError for non-dictionaries
                return self._Undefined(part)

        return value


Instruction:
Write a Python 3 script for
executing the program and return the expanded token list.

Response:
def execute(self, data_dict, callback, group=None, trace=None):
        """Low level method to expand the template piece by piece.

    Args:
      data_dict: The JSON data dictionary.
      callback: A callback which should be called with each expanded token.
      group: Dictionary of name -> Template instance (for styles)

    Example: You can pass 'f.write' as the callback to write directly to a file
    handle.
    """
        # First try the passed in version, then the one set by _SetTemplateGroup.  May
        # be None.  Only one of these should be set.
        group = group or self.group
        context = _ScopedContext(data_dict, self.undefined_str, group=group)
        _Execute(self._program.Statements(), context, callback, trace)


Instruction:
Can you generate a brief explanation for the following Python 3 code
def expand(self, *args, **kwargs):
        if args:
            if len(args) == 1:
                data_dict = args[0]
                trace = kwargs.get('trace')
                style = kwargs.get('style')
            else:
                raise TypeError(
                    'expand() only takes 1 positional argument (got %s)' % args)
        else:
            data_dict = kwargs
            trace = None  # Can't use trace= with the kwargs style
            style = None

        tokens = []
        group = _MakeGroupFromRootSection(self._program, self.undefined_str)
        if style:
            style.execute(data_dict, tokens.append, group=group,
                          trace=trace)
        else:
            # Needs a group to reference its OWN {.define}s
            self.execute(data_dict, tokens.append, group=group,
                         trace=trace)

        return JoinTokens(tokens)

Response: Expands the template with the given data dictionary returning a string.


Instruction:
Write a Python 3 function that can
yield a list of tokens resulting from expansion.

Response:
def tokenstream(self, data_dict):
        """Yields a list of tokens resulting from expansion.

    This may be useful for WSGI apps.  NOTE: In the current implementation, the
    entire expanded template must be stored memory.

    NOTE: This is a generator, but JavaScript doesn't have generators.
    """
        tokens = []
        self.execute(data_dict, tokens.append)
        for token in tokens:
            yield token


Instruction:
How would you code a function in Python 3 to
align an unwrapped phase array to zero - phase array.

Response:
def align_unwrapped(sino):
    """Align an unwrapped phase array to zero-phase

    All operations are performed in-place.
    """
    samples = []
    if len(sino.shape) == 2:
        # 2D
        # take 1D samples at beginning and end of array
        samples.append(sino[:, 0])
        samples.append(sino[:, 1])
        samples.append(sino[:, 2])
        samples.append(sino[:, -1])
        samples.append(sino[:, -2])

    elif len(sino.shape) == 3:
        # 3D
        # take 1D samples at beginning and end of array
        samples.append(sino[:, 0, 0])
        samples.append(sino[:, 0, -1])
        samples.append(sino[:, -1, 0])
        samples.append(sino[:, -1, -1])
        samples.append(sino[:, 0, 1])

    # find discontinuities in the samples
    steps = np.zeros((len(samples), samples[0].shape[0]))
    for i in range(len(samples)):
        t = np.unwrap(samples[i])
        steps[i] = samples[i] - t

    # if the majority believes so, add a step of PI
    remove = mode(steps, axis=0)[0][0]

    # obtain divmod min
    twopi = 2*np.pi
    minimum = divmod_neg(np.min(sino), twopi)[0]
    remove += minimum*twopi

    for i in range(len(sino)):
        sino[i] -= remove[i]


Instruction:
Write a Python 3 function for
returning divmod with closest result to zero

Response:
def divmod_neg(a, b):
    """Return divmod with closest result to zero"""
    q, r = divmod(a, b)
    # make sure r is close to zero
    sr = np.sign(r)
    if np.abs(r) > b/2:
        q += sr
        r -= b * sr
    return q, r


Instruction:
Make a summary of the following Python 3 code
def json_encoder_default(obj):
    if np is not None and hasattr(obj, 'size') and hasattr(obj, 'dtype'):
        if obj.size == 1:
            if np.issubdtype(obj.dtype, np.integer):
                return int(obj)
            elif np.issubdtype(obj.dtype, np.floating):
                return float(obj)

    if isinstance(obj, set):
        return list(obj)
    elif hasattr(obj, 'to_native'):
        # DatastoreList, DatastoreDict
        return obj.to_native()
    elif hasattr(obj, 'tolist') and hasattr(obj, '__iter__'):
        # for np.array
        return obj.tolist()

    return obj

Response: Default JSON encoder for more data types than the default JSON encoder.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def fig_to_src(figure, image_format='png', dpi=80):
    if image_format == 'png':
        f = io.BytesIO()
        figure.savefig(f, format=image_format, dpi=dpi)
        f.seek(0)
        return png_to_src(f.read())

    elif image_format == 'svg':
        f = io.StringIO()
        figure.savefig(f, format=image_format, dpi=dpi)
        f.seek(0)
        return svg_to_src(f.read())

Response: Convert a matplotlib figure to an inline HTML image.


Instruction:
Can you tell what is the following Python 3 function doing
def Reset(self):
        ' Reset Axis and set default parameters for H-bridge '
        spi.SPI_write(self.CS, [0xC0, 0x60])      # reset
#        spi.SPI_write(self.CS, [0x14, 0x14])      # Stall Treshold setup
#        spi.SPI_write(self.CS, [0xFF, 0xFF])
#        spi.SPI_write(self.CS, [0x13, 0x13])      # Over Current Treshold setup
#        spi.SPI_write(self.CS, [0xFF, 0xFF])
        spi.SPI_write(self.CS, [0x15, 0xFF])      # Full Step speed
        spi.SPI_write(self.CS, [0xFF, 0xFF])
        spi.SPI_write(self.CS, [0xFF, 0xFF])
        spi.SPI_write(self.CS, [0x05, 0x05])      # ACC
        spi.SPI_write(self.CS, [0x01, 0x01])
        spi.SPI_write(self.CS, [0xF5, 0xF5])
        spi.SPI_write(self.CS, [0x06, 0x06])      # DEC
        spi.SPI_write(self.CS, [0x01, 0x01])
        spi.SPI_write(self.CS, [0xF5, 0xF5])
        spi.SPI_write(self.CS, [0x0A, 0x0A])      # KVAL_RUN
        spi.SPI_write(self.CS, [0x10, 0x10])
        spi.SPI_write(self.CS, [0x0B, 0x0B])      # KVAL_ACC
        spi.SPI_write(self.CS, [0x20, 0x20])
        spi.SPI_write(self.CS, [0x0C, 0x0C])      # KVAL_DEC
        spi.SPI_write(self.CS, [0x20, 0x20])
        spi.SPI_write(self.CS, [0x18, 0x18])      # CONFIG
        spi.SPI_write(self.CS, [0b00111000, 0b00111000])
        spi.SPI_write(self.CS, [0b00000000, 0b00000000])

Response: Reset Axis and set default parameters for H - bridge


Instruction:
Can you generate the documentation for the following Python 3 function
def MaxSpeed(self, speed):
        ' Setup of maximum speed '
        spi.SPI_write(self.CS, [0x07, 0x07])       # Max Speed setup
        spi.SPI_write(self.CS, [0x00, 0x00])
        spi.SPI_write(self.CS, [speed, speed])

Response: Setup of maximum speed


Instruction:
Can you implement a function in Python 3 that
goes away from Limit Switch

Response:
def ReleaseSW(self):
        ' Go away from Limit Switch '
        while self.ReadStatusBit(2) == 1:           # is Limit Switch ON ?
            spi.SPI_write(self.CS, [0x92, 0x92] | (~self.Dir & 1))     # release SW
            while self.IsBusy():
                pass
            self.MoveWait(10)


Instruction:
Create a Python 3 function for
moving some distance units from current position

Response:
def Move(self, units):
        ' Move some distance units from current position '
        steps = units * self.SPU  # translate units to steps
        if steps > 0:                                          # look for direction
            spi.SPI_write(self.CS, [0x40 | (~self.Dir & 1), 0x40 | (~self.Dir & 1)])
        else:
            spi.SPI_write(self.CS, [0x40 | (self.Dir & 1), 0x40 | (self.Dir & 1)])
        steps = int(abs(steps))
        spi.SPI_write(self.CS, [(steps >> 16) & 0xFF, (steps >> 16) & 0xFF])
        spi.SPI_write(self.CS, [(steps >> 8) & 0xFF, (steps >> 8) & 0xFF])
        spi.SPI_write(self.CS, [steps & 0xFF, steps & 0xFF])


Instruction:
Explain what the following Python 3 code does
def bbox(coordinates, crs, outname=None, format='ESRI Shapefile', overwrite=True):
    srs = crsConvert(crs, 'osr')

    ring = ogr.Geometry(ogr.wkbLinearRing)

    ring.AddPoint(coordinates['xmin'], coordinates['ymin'])
    ring.AddPoint(coordinates['xmin'], coordinates['ymax'])
    ring.AddPoint(coordinates['xmax'], coordinates['ymax'])
    ring.AddPoint(coordinates['xmax'], coordinates['ymin'])
    ring.CloseRings()

    geom = ogr.Geometry(ogr.wkbPolygon)
    geom.AddGeometry(ring)

    geom.FlattenTo2D()

    bbox = Vector(driver='Memory')
    bbox.addlayer('bbox', srs, geom.GetGeometryType())
    bbox.addfield('area', ogr.OFTReal)
    bbox.addfeature(geom, fields={'area': geom.Area()})
    geom = None
    if outname is None:
        return bbox
    else:
        bbox.write(outname, format, overwrite)

Response: Create a bounding box vector object from a dictionary of coordinates and coordinate reference system.


Instruction:
Write a Python 3 script to
dissolve the polygons of a vector file by an attribute field Parameters ---------- infile: str the input vector file outfile: str the output shapefile field: str the field name to merge the polygons by layername: str the name of the output vector layer; If set to None the layername will be the basename of infile without extension Returns -------

Response:
def dissolve(infile, outfile, field, layername=None):
    """
    dissolve the polygons of a vector file by an attribute field
    Parameters
    ----------
    infile: str
        the input vector file
    outfile: str
        the output shapefile
    field: str
        the field name to merge the polygons by
    layername: str
        the name of the output vector layer;
        If set to None the layername will be the basename of infile without extension

    Returns
    -------

    """
    with Vector(infile) as vec:
        srs = vec.srs
        feat = vec.layer[0]
        d = feat.GetFieldDefnRef(field)
        width = d.width
        type = d.type
        feat = None

    layername = layername if layername is not None else os.path.splitext(os.path.basename(infile))[0]

    # the following can be used if GDAL was compiled with the spatialite extension
    # not tested, might need some additional/different lines
    # with Vector(infile) as vec:
    #     vec.vector.ExecuteSQL('SELECT ST_Union(geometry), {0} FROM {1} GROUP BY {0}'.format(field, vec.layername),
    #                          dialect='SQLITE')
    #     vec.write(outfile)

    conn = sqlite_setup(extensions=['spatialite', 'gdal'])
    conn.execute('CREATE VIRTUAL TABLE merge USING VirtualOGR("{}");'.format(infile))
    select = conn.execute('SELECT {0},asText(ST_Union(geometry)) as geometry FROM merge GROUP BY {0};'.format(field))
    fetch = select.fetchall()
    with Vector(driver='Memory') as merge:
        merge.addlayer(layername, srs, ogr.wkbPolygon)
        merge.addfield(field, type=type, width=width)
        for i in range(len(fetch)):
            merge.addfeature(ogr.CreateGeometryFromWkt(fetch[i][1]), {field: fetch[i][0]})
        merge.write(outfile)
    conn.close()


Instruction:
How would you code a function in Python 3 to
create a Vector object from ogr features

Response:
def feature2vector(feature, ref, layername=None):
    """
    create a Vector object from ogr features

    Parameters
    ----------
    feature: list of :osgeo:class:`ogr.Feature` or :osgeo:class:`ogr.Feature`
        a single feature or a list of features
    ref: Vector
        a reference Vector object to retrieve geo information from
    layername: str or None
        the name of the output layer; retrieved from `ref` if `None`

    Returns
    -------
    Vector
        the new Vector object
    """
    features = feature if isinstance(feature, list) else [feature]
    layername = layername if layername is not None else ref.layername
    vec = Vector(driver='Memory')
    vec.addlayer(layername, ref.srs, ref.geomType)
    feat_def = features[0].GetDefnRef()
    fields = [feat_def.GetFieldDefn(x) for x in range(0, feat_def.GetFieldCount())]
    vec.layer.CreateFields(fields)
    for feat in features:
        vec.layer.CreateFeature(feat)
    vec.init_features()
    return vec


Instruction:
Write a Python 3 function for
returning the intersection of two Vector objects

Response:
def intersect(obj1, obj2):
    """
    intersect two Vector objects

    Parameters
    ----------
    obj1: Vector
        the first vector object; this object is reprojected to the CRS of obj2 if necessary
    obj2: Vector
        the second vector object

    Returns
    -------
    Vector
        the intersect of obj1 and obj2
    """
    if not isinstance(obj1, Vector) or not isinstance(obj2, Vector):
        raise RuntimeError('both objects must be of type Vector')

    obj1 = obj1.clone()
    obj2 = obj2.clone()

    obj1.reproject(obj2.srs)

    #######################################################
    # create basic overlap
    union1 = ogr.Geometry(ogr.wkbMultiPolygon)
    # union all the geometrical features of layer 1
    for feat in obj1.layer:
        union1.AddGeometry(feat.GetGeometryRef())
    obj1.layer.ResetReading()
    union1.Simplify(0)
    # same for layer2
    union2 = ogr.Geometry(ogr.wkbMultiPolygon)
    for feat in obj2.layer:
        union2.AddGeometry(feat.GetGeometryRef())
    obj2.layer.ResetReading()
    union2.Simplify(0)
    # intersection
    intersect_base = union1.Intersection(union2)
    union1 = None
    union2 = None
    #######################################################
    # compute detailed per-geometry overlaps
    if intersect_base.GetArea() > 0:
        intersection = Vector(driver='Memory')
        intersection.addlayer('intersect', obj1.srs, ogr.wkbPolygon)
        fieldmap = []
        for index, fielddef in enumerate([obj1.fieldDefs, obj2.fieldDefs]):
            for field in fielddef:
                name = field.GetName()
                i = 2
                while name in intersection.fieldnames:
                    name = '{}_{}'.format(field.GetName(), i)
                    i += 1
                fieldmap.append((index, field.GetName(), name))
                intersection.addfield(name, type=field.GetType(), width=field.GetWidth())

        for feature1 in obj1.layer:
            geom1 = feature1.GetGeometryRef()
            if geom1.Intersects(intersect_base):
                for feature2 in obj2.layer:
                    geom2 = feature2.GetGeometryRef()
                    # select only the intersections
                    if geom2.Intersects(intersect_base):
                        intersect = geom2.Intersection(geom1)
                        fields = {}
                        for item in fieldmap:
                            if item[0] == 0:
                                fields[item[2]] = feature1.GetField(item[1])
                            else:
                                fields[item[2]] = feature2.GetField(item[1])
                        intersection.addfeature(intersect, fields)
        intersect_base = None
        return intersection


Instruction:
Can you generate a brief explanation for the following Python 3 code
def addfeature(self, geometry, fields=None):

        feature = ogr.Feature(self.layerdef)
        feature.SetGeometry(geometry)

        if fields is not None:
            for fieldname, value in fields.items():
                if fieldname not in self.fieldnames:
                    raise IOError('field "{}" is missing'.format(fieldname))
                try:
                    feature.SetField(fieldname, value)
                except NotImplementedError as e:
                    fieldindex = self.fieldnames.index(fieldname)
                    fieldtype = feature.GetFieldDefnRef(fieldindex).GetTypeName()
                    message = str(e) + '\ntrying to set field {} (type {}) to value {} (type {})'
                    message = message.format(fieldname, fieldtype, value, type(value))
                    raise(NotImplementedError(message))

        self.layer.CreateFeature(feature)
        feature = None
        self.init_features()

Response: add a feature to the vector object from a geometry


Instruction:
Can you generate the documentation for the following Python 3 function
def addfield(self, name, type, width=10):
        fieldDefn = ogr.FieldDefn(name, type)
        if type == ogr.OFTString:
            fieldDefn.SetWidth(width)
        self.layer.CreateField(fieldDefn)

Response: add a field to the vector layer


Instruction:
Write a Python 3 function that can
add a layer to the vector layer

Response:
def addlayer(self, name, srs, geomType):
        """
        add a layer to the vector layer

        Parameters
        ----------
        name: str
            the layer name
        srs: int, str or :osgeo:class:`osr.SpatialReference`
            the spatial reference system. See :func:`spatialist.auxil.crsConvert` for options.
        geomType: int
            an OGR well-known binary data type.
            See `Module ogr <https://gdal.org/python/osgeo.ogr-module.html>`_.

        Returns
        -------

        """
        self.vector.CreateLayer(name, srs, geomType)
        self.init_layer()


Instruction:
Write a Python 3 function for
adding a vector object to the current Vector object

Response:
def addvector(self, vec):
        """
        add a vector object to the layer of the current Vector object

        Parameters
        ----------
        vec: Vector
            the vector object to add
        merge: bool
            merge overlapping polygons?

        Returns
        -------

        """
        vec.layer.ResetReading()
        for feature in vec.layer:
            self.layer.CreateFeature(feature)
        self.init_features()
        vec.layer.ResetReading()


Instruction:
Can you implement a function in Python 3 that
creates a bounding box from the extent of the Vector object.

Response:
def bbox(self, outname=None, format='ESRI Shapefile', overwrite=True):
        """
        create a bounding box from the extent of the Vector object

        Parameters
        ----------
        outname: str or None
            the name of the vector file to be written; if None, a Vector object is returned
        format: str
            the name of the file format to write
        overwrite: bool
            overwrite an already existing file?

        Returns
        -------
        Vector or None
            if outname is None, the bounding box Vector object
        """
        if outname is None:
            return bbox(self.extent, self.srs)
        else:
            bbox(self.extent, self.srs, outname=outname, format=format, overwrite=overwrite)


Instruction:
Can you create a Python 3 function that
exports the geometry of each feature as a wkt string

Response:
def convert2wkt(self, set3D=True):
        """
        export the geometry of each feature as a wkt string

        Parameters
        ----------
        set3D: bool
            keep the third (height) dimension?

        Returns
        -------

        """
        features = self.getfeatures()
        for feature in features:
            try:
                feature.geometry().Set3D(set3D)
            except AttributeError:
                dim = 3 if set3D else 2
                feature.geometry().SetCoordinateDimension(dim)

        return [feature.geometry().ExportToWkt() for feature in features]


Instruction:
Create a Python 3 function for
getting features by field attribute

Response:
def getFeatureByAttribute(self, fieldname, attribute):
        """
        get features by field attribute

        Parameters
        ----------
        fieldname: str
            the name of the queried field
        attribute: int or str
            the field value of interest

        Returns
        -------
        list of :osgeo:class:`ogr.Feature` or :osgeo:class:`ogr.Feature`
            the feature(s) matching the search query
        """
        attr = attribute.strip() if isinstance(attribute, str) else attribute
        if fieldname not in self.fieldnames:
            raise KeyError('invalid field name')
        out = []
        self.layer.ResetReading()
        for feature in self.layer:
            field = feature.GetField(fieldname)
            field = field.strip() if isinstance(field, str) else field
            if field == attr:
                out.append(feature.Clone())
        self.layer.ResetReading()
        if len(out) == 0:
            return None
        elif len(out) == 1:
            return out[0]
        else:
            return out


Instruction:
Can you tell what is the following Python 3 function doing
def getFeatureByIndex(self, index):
        feature = self.layer[index]
        if feature is None:
            feature = self.getfeatures()[index]
        return feature

Response: get a feature by its index


Instruction:
Can you tell what is the following Python 3 function doing
def init_layer(self):
        self.layer = self.vector.GetLayer()
        self.__features = [None] * self.nfeatures

Response: initialize a layer object


Instruction:
How would you code a function in Python 3 to
load all features into memory

Response:
def load(self):
        """
        load all feature into memory

        Returns
        -------

        """
        self.layer.ResetReading()
        for i in range(self.nfeatures):
            if self.__features[i] is None:
                self.__features[i] = self.layer[i]


Instruction:
Can you generate the documentation for the following Python 3 function
def reproject(self, projection):
        srs_out = crsConvert(projection, 'osr')

        if self.srs.IsSame(srs_out) == 0:

            # create the CoordinateTransformation
            coordTrans = osr.CoordinateTransformation(self.srs, srs_out)

            layername = self.layername
            geomType = self.geomType
            features = self.getfeatures()
            feat_def = features[0].GetDefnRef()
            fields = [feat_def.GetFieldDefn(x) for x in range(0, feat_def.GetFieldCount())]

            self.__init__()
            self.addlayer(layername, srs_out, geomType)
            self.layer.CreateFields(fields)

            for feature in features:
                geom = feature.GetGeometryRef()
                geom.Transform(coordTrans)
                newfeature = feature.Clone()
                newfeature.SetGeometry(geom)
                self.layer.CreateFeature(newfeature)
                newfeature = None
            self.init_features()

Response: Reproject the object to the target CRS.


Instruction:
Here you have a function in Python 3, explain what it does
def setCRS(self, crs):
        # try to convert the input crs to osr.SpatialReference
        srs_out = crsConvert(crs, 'osr')

        # save all relevant info from the existing vector object
        layername = self.layername
        geomType = self.geomType
        layer_definition = ogr.Feature(self.layer.GetLayerDefn())
        fields = [layer_definition.GetFieldDefnRef(x) for x in range(layer_definition.GetFieldCount())]
        features = self.getfeatures()

        # initialize a new vector object and create a layer
        self.__init__()
        self.addlayer(layername, srs_out, geomType)

        # add the fields to new layer
        self.layer.CreateFields(fields)

        # add the features to the newly created layer
        for feat in features:
            self.layer.CreateFeature(feat)
        self.init_features()

Response: This method is used to reset the spatial reference system of the vector object.


Instruction:
How would you explain what the following Python 3 function does
def write(self, outfile, format='ESRI Shapefile', overwrite=True):
        (outfilepath, outfilename) = os.path.split(outfile)
        basename = os.path.splitext(outfilename)[0]

        driver = ogr.GetDriverByName(format)

        if os.path.exists(outfile):
            if overwrite:
                driver.DeleteDataSource(outfile)
            else:
                raise RuntimeError('target file already exists')

        outdataset = driver.CreateDataSource(outfile)
        outlayer = outdataset.CreateLayer(self.layername, geom_type=self.geomType)
        outlayerdef = outlayer.GetLayerDefn()

        for fieldDef in self.fieldDefs:
            outlayer.CreateField(fieldDef)

        self.layer.ResetReading()
        for feature in self.layer:
            outFeature = ogr.Feature(outlayerdef)
            outFeature.SetGeometry(feature.GetGeometryRef())
            for name in self.fieldnames:
                outFeature.SetField(name, feature.GetField(name))
            # add the feature to the shapefile
            outlayer.CreateFeature(outFeature)
            outFeature = None
        self.layer.ResetReading()

        if format == 'ESRI Shapefile':
            srs_out = self.srs.Clone()
            srs_out.MorphToESRI()
            with open(os.path.join(outfilepath, basename + '.prj'), 'w') as prj:
                prj.write(srs_out.ExportToWkt())

        outdataset = None

Response: Writes the Vector object to a file.


Instruction:
Can you generate the documentation for the following Python 3 function
def write_byte_data(self, address, register, value):
        return self.smbus.write_byte_data(address, register, value)

Response: Write a single byte to a designated register in a specific location.


Instruction:
Explain what the following Python 3 code does
def write_word_data(self, address, register, value):
        return self.smbus.write_word_data(address, register, value)

Response: Write 2 bytes to a specified register in a specific location.


Instruction:
Here you have a function in Python 3, explain what it does
def write_block_data(self, address, register, value):
        return self.smbus.write_block_data(address, register, value)

Response: Write to a specific register in a specific SMBus device.


Instruction:
Given the following Python 3 function, write the documentation
def block_process_call(self, address, register, value):
        return self.smbus.block_process_call(address, register, value)

Response: This function is used to send a block read call to the specified address and value.


Instruction:
Explain what the following Python 3 code does
def write_i2c_block_data(self, address, register, value):
        return self.smbus.write_i2c_block_data(address, register, value)

Response: Write to a specific register in a specific I2C block.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def _init_zmq(self, port_publish, port_subscribe):

        log.debug('kernel {} publishing on port {}'
                  ''.format(self.analysis.id_, port_publish))
        self.zmq_publish = zmq.Context().socket(zmq.PUB)
        self.zmq_publish.connect('tcp://127.0.0.1:{}'.format(port_publish))

        log.debug('kernel {} subscribed on port {}'
                  ''.format(self.analysis.id_, port_subscribe))
        self.zmq_sub_ctx = zmq.Context()
        self.zmq_sub = self.zmq_sub_ctx.socket(zmq.SUB)
        self.zmq_sub.setsockopt(zmq.SUBSCRIBE,
                                self.analysis.id_.encode('utf-8'))
        self.zmq_sub.connect('tcp://127.0.0.1:{}'.format(port_subscribe))

        self.zmq_stream_sub = zmq.eventloop.zmqstream.ZMQStream(self.zmq_sub)
        self.zmq_stream_sub.on_recv(self.zmq_listener)

        # send zmq handshakes until a zmq ack is received
        self.zmq_ack = False
        self.send_handshake()

Response: Initialize the zmq messaging.


Instruction:
Explain what the following Python 3 code does
def run_process(self, analysis, action_name, message='__nomessagetoken__'):

        # detect process_id
        process_id = None
        if isinstance(message, dict) and '__process_id' in message:
            process_id = message['__process_id']
            del message['__process_id']

        if process_id:
            analysis.emit('__process', {'id': process_id, 'status': 'start'})

        fns = [
            functools.partial(class_fn, analysis)
            for class_fn in (analysis._action_handlers.get(action_name, []) +
                             analysis._action_handlers.get('*', []))
        ]
        if fns:
            args, kwargs = [], {}

            # Check whether this is a list (positional arguments)
            # or a dictionary (keyword arguments).
            if isinstance(message, list):
                args = message
            elif isinstance(message, dict):
                kwargs = message
            elif message == '__nomessagetoken__':
                pass
            else:
                args = [message]

            for fn in fns:
                log.debug('kernel calling {}'.format(fn))
                fn(*args, **kwargs)
        else:
            # default is to store action name and data as key and value
            # in analysis.data
            #
            # TODO(sven): deprecate this in favor of set_state() in Analysis
            # with new Datastore
            value = message if message != '__nomessagetoken__' else None
            if hasattr(analysis.data, 'set_state'):
                # TODO(sven): add deprecation warning here?
                analysis.data.set_state({action_name: value})
            else:
                # TODO(sven): add deprecation warning here?
                analysis.data[action_name] = value

        log.debug('kernel done {}'.format(action_name))

        if process_id:
            analysis.emit('__process', {'id': process_id, 'status': 'end'})

        if action_name == 'disconnected':
            log.debug('kernel {} shutting down'.format(analysis.id_))
            self.zmq_publish.close()

            self.zmq_stream_sub.close()
            self.zmq_sub.close()
            self.zmq_sub_ctx.destroy()

Response: Executes an action in the analysis with the given message.


Instruction:
Can you generate the documentation for the following Python 3 function
def emit(self, signal, message, analysis_id):

        log.debug('kernel {} zmq send ({}): {}'
                  ''.format(analysis_id, signal, message))
        self.zmq_publish.send(json.dumps({
            'analysis_id': analysis_id,
            'frame': {'signal': signal, 'load': message},
        }, default=json_encoder_default).encode('utf-8'))

Response: Emit a signal to main.


Instruction:
Make a summary of the following Python 3 code
def set_ports(self, port0 = 0x00, port1 = 0x00):
        'Writes specified value to the pins defined as output by config_ports() method. Writing to input pins has no effect.'
        self.bus.write_byte_data(self.address, self.OUTPUT_PORT0, port0)
        self.bus.write_byte_data(self.address, self.OUTPUT_PORT1, port1)
        return True

Response: Writes specified value to the pins defined as output by config_ports() method. Writing to input pins has no effect.


Instruction:
Make a summary of the following Python 3 code
def get_ports(self):
        'Reads logical values at pins.'
        return (self.bus.read_byte_data(self.address, self.STATUS_PORT0), self.bus.read_byte_data(self.address, self.STATUS_PORT1));

Response: Reads logical values at pins.


Instruction:
Implement a function in Python 3 to
read logical values at pins.

Response:
def get_config(self):
        'Reads logical values at pins.'
        return (self.bus.read_byte_data(self.address, self.CONTROL_PORT0), self.bus.read_byte_data(self.address, self.CONTROL_PORT1));


Instruction:
How would you explain what the following Python 3 function does
def set_pullups(self, port0 = 0x00, port1 = 0x00):
        'Sets INPUT (1) or OUTPUT (0) direction on pins. Inversion setting is applicable for input pins  1-inverted 0-noninverted input polarity.'
        self.bus.write_byte_data(self.address, self.PULLUP_PORT0, port0)
        self.bus.write_byte_data(self.address, self.PULLUP_PORT1, port1)
        return

Response: Sets INPUT 1 or OUTPUT 0 direction on pins. Inversion setting is applicable for input pins  1 - inverted 0 - noninverted input polarity.


Instruction:
Given the following Python 3 function, write the documentation
def set_ports(self, port0 = 0x00, port1 = 0x00):
        'Writes specified value to the pins defined as output by method. Writing to input pins has no effect.'
        self.bus.write_byte_data(self.address, self.CONTROL_PORT0, port0)
        self.bus.write_byte_data(self.address, self.CONTROL_PORT0, port1)
        return

Response: Writes specified value to the pins defined as output by method. Writing to input pins has no effect.


Instruction:
How would you explain what the following Python 3 function does
def get_gtf_db(gtf, in_memory=False):
    db_file = gtf + '.db'
    if gtf.endswith('.gz'):
        db_file = gtf[:-3] + '.db'
    if file_exists(db_file):
        return gffutils.FeatureDB(db_file)
    db_file = ':memory:' if in_memory else db_file
    if in_memory or not file_exists(db_file):
        debug('GTF database does not exist, creating...')
        infer_extent = guess_infer_extent(gtf)
        db = gffutils.create_db(gtf, dbfn=db_file,
                                infer_gene_extent=infer_extent)
        return db
    else:
        return gffutils.FeatureDB(db_file)

Response: get a gffutils DB from a GTF file


Instruction:
How would you explain what the following Python 3 function does
def gtf_to_bed(gtf, alt_out_dir=None):
    out_file = os.path.splitext(gtf)[0] + '.bed'
    if file_exists(out_file):
        return out_file
    if not os.access(os.path.dirname(out_file), os.W_OK | os.X_OK):
        if not alt_out_dir:
            raise IOError('Cannot write transcript BED output file %s' % out_file)
        else:
            out_file = os.path.join(alt_out_dir, os.path.basename(out_file))
    with open(out_file, "w") as out_handle:
        db = get_gtf_db(gtf)
        for feature in db.features_of_type('transcript', order_by=("seqid", "start", "end")):
            chrom = feature.chrom
            start = feature.start
            end = feature.end
            attributes = feature.attributes.keys()
            strand = feature.strand
            name = (feature['gene_name'][0] if 'gene_name' in attributes else
                    feature['gene_id'][0])
            line = "\t".join([str(x) for x in [chrom, start, end, name, ".",
                                               strand]])
            out_handle.write(line + "\n")
    return out_file

Response: Convert GTF to BED file.


Instruction:
Can you write a function in Python 3 where it
writes out a file of transcript - > gene mappings for a GTF file

Response:
def tx2genefile(gtf, out_file=None):
    """
    write out a file of transcript->gene mappings.
    use the installed tx2gene.csv if it exists, else write a new one out
    """
    installed_tx2gene = os.path.join(os.path.dirname(gtf), "tx2gene.csv")
    if file_exists(installed_tx2gene):
        return installed_tx2gene
    if file_exists(out_file):
        return out_file
    with file_transaction(out_file) as tx_out_file:
        with open(tx_out_file, "w") as out_handle:
            for k, v in transcript_to_gene(gtf).items():
                out_handle.write(",".join([k, v]) + "\n")
    return out_file


Instruction:
Explain what the following Python 3 code does
def transcript_to_gene(gtf):
    gene_lookup = {}
    for feature in complete_features(get_gtf_db(gtf)):
        gene_id = feature.attributes.get('gene_id', [None])[0]
        transcript_id = feature.attributes.get('transcript_id', [None])[0]
        gene_lookup[transcript_id] = gene_id
    return gene_lookup

Response: return a dictionary keyed by transcript_id of the associated gene_id


Instruction:
Write a Python 3 script for
setting new output frequency for the current entry.

Response:
def set_freq(self, fout, freq):
        """
        Sets new output frequency, required parameters are real current frequency at output and new required frequency.
        """
        hsdiv_tuple = (4, 5, 6, 7, 9, 11)           # possible dividers
        n1div_tuple = (1,) + tuple(range(2,129,2))  #
        fdco_min = 5670.0           # set maximum as minimum
        hsdiv = self.get_hs_div()   # read curent dividers
        n1div = self.get_n1_div()   #

        if abs((freq-fout)*1e6/fout) > 3500:
            # Large change of frequency
            fdco = fout * hsdiv * n1div # calculate high frequency oscillator
            fxtal =  fdco / self.get_rfreq()  # should be fxtal = 114.285

            for hsdiv_iter in hsdiv_tuple:      # find dividers with minimal power consumption
                for n1div_iter in n1div_tuple:
                    fdco_new = freq * hsdiv_iter * n1div_iter
                    if (fdco_new >= 4850) and (fdco_new <= 5670):
                        if (fdco_new <= fdco_min):
                            fdco_min = fdco_new
                            hsdiv = hsdiv_iter
                            n1div = n1div_iter
            rfreq = fdco_min / fxtal

            self.freeze_dco()       # write registers
            self.set_hs_div(hsdiv)
            self.set_n1_div(n1div)
            self.set_rfreq(rfreq)
            self.unfreeze_dco()
            self.new_freq()
        else:
            # Small change of frequency
            rfreq = self.get_rfreq() * (freq/fout)

            self.freeze_m()         # write registers
            self.set_rfreq(rfreq)
            self.unfreeze_m()


Instruction:
Can you tell what is the following Python 3 function doing
def _get_file_alignment_for_new_binary_file(self, file: File) -> int:
        if len(file.data) <= 0x20:
            return 0
        bom = file.data[0xc:0xc+2]
        if bom != b'\xff\xfe' and bom != b'\xfe\xff':
            return 0

        be = bom == b'\xfe\xff'
        file_size: int = struct.unpack_from(_get_unpack_endian_character(be) + 'I', file.data, 0x1c)[0]
        if len(file.data) != file_size:
            return 0
        return 1 << file.data[0xe]

Response: Detects alignment requirements for binary files with new nn. util. BinaryFileHeader.


Instruction:
Given the following Python 3 function, write the documentation
def create_analyses(name, kernel=None):

    if not os.path.exists(os.path.join(os.getcwd(), 'analyses')):
        os.system("mkdir analyses")

    # __init__.py
    init_path = os.path.join(os.getcwd(), 'analyses', '__init__.py')
    if not os.path.exists(init_path):
        with open(init_path, 'w') as f:
            pass

    # index.yaml
    index_path = os.path.join(os.getcwd(), 'analyses', 'index.yaml')
    if not os.path.exists(index_path):
        with open(index_path, 'w') as f:
            f.write('title: Analyses\n')
            f.write('description: A short description.\n')
            f.write('version: 0.1.0\n')
            f.write('\n')
            f.write('analyses:\n')

    if kernel is None:
        with open(index_path, 'a') as f:
            f.write('  # automatically inserted by scaffold-databench\n')
            f.write('  - name: {}\n'.format(name))
            f.write('    title: {}\n'.format(name.title()))
            f.write('    description: A new analysis.\n')
            f.write('    watch:\n')
            f.write('      - {}/*.js\n'.format(name))
            f.write('      - {}/*.html\n'.format(name))

Response: Create an analysis with given name and suffix.


Instruction:
Make a summary of the following Python 3 code
def crsConvert(crsIn, crsOut):
    if isinstance(crsIn, osr.SpatialReference):
        srs = crsIn.Clone()
    else:
        srs = osr.SpatialReference()

        if isinstance(crsIn, int):
            crsIn = 'EPSG:{}'.format(crsIn)

        if isinstance(crsIn, str):
            try:
                srs.SetFromUserInput(crsIn)
            except RuntimeError:
                raise TypeError('crsIn not recognized; must be of type WKT, PROJ4 or EPSG')
        else:
            raise TypeError('crsIn must be of type int, str or osr.SpatialReference')
    if crsOut == 'wkt':
        return srs.ExportToWkt()
    elif crsOut == 'prettyWkt':
        return srs.ExportToPrettyWkt()
    elif crsOut == 'proj4':
        return srs.ExportToProj4()
    elif crsOut == 'epsg':
        srs.AutoIdentifyEPSG()
        return int(srs.GetAuthorityCode(None))
    elif crsOut == 'opengis':
        srs.AutoIdentifyEPSG()
        return 'http://www.opengis.net/def/crs/EPSG/0/{}'.format(srs.GetAuthorityCode(None))
    elif crsOut == 'osr':
        return srs
    else:
        raise ValueError('crsOut not recognized; must be either wkt, proj4, opengis or epsg')

Response: Convert two types of spatial references to one type of spatial reference.


Instruction:
Implement a Python 3 function for
computing the distance in meters between two points in latlon

Response:
def haversine(lat1, lon1, lat2, lon2):
    """
    compute the distance in meters between two points in latlon

    Parameters
    ----------
    lat1: int or float
        the latitude of point 1
    lon1: int or float
        the longitude of point 1
    lat2: int or float
        the latitude of point 2
    lon2: int or float
        the longitude of point 2

    Returns
    -------
    float
        the distance between point 1 and point 2 in meters

    """
    radius = 6371000
    lat1, lon1, lat2, lon2 = map(math.radians, [lat1, lon1, lat2, lon2])
    a = math.sin((lat2 - lat1) / 2) ** 2 + math.cos(lat1) * math.cos(lat2) * math.sin((lon2 - lon1) / 2) ** 2
    c = 2 * math.asin(math.sqrt(a))
    return radius * c


Instruction:
Write a Python 3 function that can
wrap for gdal. Warp

Response:
def gdalwarp(src, dst, options):
    """
    a simple wrapper for :osgeo:func:`gdal.Warp`

    Parameters
    ----------
    src: str, :osgeo:class:`ogr.DataSource` or :osgeo:class:`gdal.Dataset`
        the input data set
    dst: str
        the output data set
    options: dict
        additional parameters passed to gdal.Warp; see :osgeo:func:`gdal.WarpOptions`

    Returns
    -------

    """
    try:
        out = gdal.Warp(dst, src, options=gdal.WarpOptions(**options))
    except RuntimeError as e:
        raise RuntimeError('{}:\n  src: {}\n  dst: {}\n  options: {}'.format(str(e), src, dst, options))
    out = None


Instruction:
How would you explain what the following Python 3 function does
def gdalbuildvrt(src, dst, options=None, void=True):
    options = {} if options is None else options

    if 'outputBounds' in options.keys() and gdal.__version__ < '2.4.0':
        warnings.warn('\ncreating VRT files with subsetted extent is very likely to cause problems. '
                      'Please use GDAL version >= 2.4.0, which fixed the problem.\n'
                      'see here for a description of the problem:\n'
                      '  https://gis.stackexchange.com/questions/314333/'
                      'sampling-error-using-gdalwarp-on-a-subsetted-vrt\n'
                      'and here for the release note of GDAL 2.4.0:\n'
                      '  https://trac.osgeo.org/gdal/wiki/Release/2.4.0-News')

    out = gdal.BuildVRT(dst, src, options=gdal.BuildVRTOptions(**options))
    if void:
        out = None
    else:
        return out

Response: Wrapper for GDAL. BuildVRT.


Instruction:
Write a Python 3 function for
wrapping for gdal. Translate

Response:
def gdal_translate(src, dst, options):
    """
    a simple wrapper for `gdal.Translate <https://gdal.org/python/osgeo.gdal-module.html#Translate>`_

    Parameters
    ----------
    src: str, :osgeo:class:`ogr.DataSource` or :osgeo:class:`gdal.Dataset`
        the input data set
    dst: str
        the output data set
    options: dict
        additional parameters passed to gdal.Translate;
        see `gdal.TranslateOptions <http://gdal.org/python/osgeo.gdal-module.html#TranslateOptions>`_

    Returns
    -------

    """
    out = gdal.Translate(dst, src, options=gdal.TranslateOptions(**options))
    out = None


Instruction:
Here you have a function in Python 3, explain what it does
def ogr2ogr(src, dst, options):
    out = gdal.VectorTranslate(dst, src, options=gdal.VectorTranslateOptions(**options))
    out = None

Response: Wrapper for gdal. VectorTranslate aka ogr2ogr.


Instruction:
Make a summary of the following Python 3 code
def gdal_rasterize(src, dst, options):
    out = gdal.Rasterize(dst, src, options=gdal.RasterizeOptions(**options))
    out = None

Response: Wrapper for gdal. Rasterize


Instruction:
Make a summary of the following Python 3 code
def coordinate_reproject(x, y, s_crs, t_crs):
    source = crsConvert(s_crs, 'osr')
    target = crsConvert(t_crs, 'osr')
    transform = osr.CoordinateTransformation(source, target)
    point = transform.TransformPoint(x, y)[:2]
    return point

Response: reproject a coordinate from one CRS to another


Instruction:
Create a Python 3 function to
create output_dir work_dir and log_fpath

Response:
def set_up_dirs(proc_name, output_dir=None, work_dir=None, log_dir=None):
    """ Creates output_dir, work_dir, and sets up log
    """
    output_dir = safe_mkdir(adjust_path(output_dir or join(os.getcwd(), proc_name)), 'output_dir')
    debug('Saving results into ' + output_dir)

    work_dir = safe_mkdir(work_dir or join(output_dir, 'work'), 'working directory')
    info('Using work directory ' + work_dir)

    log_fpath = set_up_log(log_dir or safe_mkdir(join(work_dir, 'log')), proc_name + '.log')

    return output_dir, work_dir, log_fpath


Instruction:
Can you tell what is the following Python 3 function doing
def _iter_lines_generator(response, decode_unicode):
    pending = None

    for chunk in _iter_content_generator(response, decode_unicode=decode_unicode):

        if pending is not None:
            chunk = pending + chunk

        lines = chunk.splitlines()

        if lines and lines[-1] and chunk and lines[-1][-1] == chunk[-1]:
            pending = lines.pop()
        else:
            pending = None

        for line in lines:
            yield line

    if pending is not None:
        yield pending

Response: Iterates over the response data one line at a time.


Instruction:
Can you implement a function in Python 3 that
raises an exception if one occurred.

Response:
def _raise_for_status(response):
    """Raises stored :class:`HTTPError`, if one occurred.

    This is the :meth:`requests.models.Response.raise_for_status` method,
    modified to add the response from Space-Track, if given.
    """

    http_error_msg = ''

    if 400 <= response.status_code < 500:
        http_error_msg = '%s Client Error: %s for url: %s' % (
            response.status_code, response.reason, response.url)

    elif 500 <= response.status_code < 600:
        http_error_msg = '%s Server Error: %s for url: %s' % (
            response.status_code, response.reason, response.url)

    if http_error_msg:
        spacetrack_error_msg = None

        try:
            json = response.json()
            if isinstance(json, Mapping):
                spacetrack_error_msg = json['error']
        except (ValueError, KeyError):
            pass

        if not spacetrack_error_msg:
            spacetrack_error_msg = response.text

        if spacetrack_error_msg:
            http_error_msg += '\nSpace-Track response:\n' + spacetrack_error_msg

        raise requests.HTTPError(http_error_msg, response=response)


Instruction:
Here you have a function in Python 3, explain what it does
def generic_request(self, class_, iter_lines=False, iter_content=False,
                        controller=None, parse_types=False, **kwargs):
        r"""Generic Space-Track query.

        The request class methods use this method internally; the public
        API is as follows:

        .. code-block:: python

            st.tle_publish(*args, **kw)
            st.basicspacedata.tle_publish(*args, **kw)
            st.file(*args, **kw)
            st.fileshare.file(*args, **kw)
            st.spephemeris.file(*args, **kw)

        They resolve to the following calls respectively:

        .. code-block:: python

            st.generic_request('tle_publish', *args, **kw)
            st.generic_request('tle_publish', *args, controller='basicspacedata', **kw)
            st.generic_request('file', *args, **kw)
            st.generic_request('file', *args, controller='fileshare', **kw)
            st.generic_request('file', *args, controller='spephemeris', **kw)

        Parameters:
            class\_: Space-Track request class name
            iter_lines: Yield result line by line
            iter_content: Yield result in 100 KiB chunks.
            controller: Optionally specify request controller to use.
            parse_types: Parse string values in response according to type given
                in predicate information, e.g. ``'2017-01-01'`` ->
                ``datetime.date(2017, 1, 1)``.
            **kwargs: These keywords must match the predicate fields on
                Space-Track. You may check valid keywords with the following
                snippet:

                .. code-block:: python

                    spacetrack = SpaceTrackClient(...)
                    spacetrack.tle.get_predicates()
                    # or
                    spacetrack.get_predicates('tle')

                See :func:`~spacetrack.operators._stringify_predicate_value` for
                which Python objects are converted appropriately.

        Yields:
            Lines—stripped of newline characters—if ``iter_lines=True``

        Yields:
            100 KiB chunks if ``iter_content=True``

        Returns:
            Parsed JSON object, unless ``format`` keyword argument is passed.

            .. warning::

                Passing ``format='json'`` will return the JSON **unparsed**. Do
                not set ``format`` if you want the parsed JSON object returned!
        """
        if iter_lines and iter_content:
            raise ValueError('iter_lines and iter_content cannot both be True')

        if 'format' in kwargs and parse_types:
            raise ValueError('parse_types can only be used if format is unset.')

        if controller is None:
            controller = self._find_controller(class_)
        else:
            classes = self.request_controllers.get(controller, None)
            if classes is None:
                raise ValueError(
                    'Unknown request controller {!r}'.format(controller))
            if class_ not in classes:
                raise ValueError(
                    'Unknown request class {!r} for controller {!r}'
                    .format(class_, controller))

        # Decode unicode unless class == download, including conversion of
        # CRLF newlines to LF.
        decode = (class_ != 'download')
        if not decode and iter_lines:
            error = (
                'iter_lines disabled for binary data, since CRLF newlines '
                'split over chunk boundaries would yield extra blank lines. '
                'Use iter_content=True instead.')
            raise ValueError(error)

        self.authenticate()

        url = ('{0}{1}/query/class/{2}'
               .format(self.base_url, controller, class_))

        offline_check = (class_, controller) in self.offline_predicates
        valid_fields = {p.name for p in self.rest_predicates}
        predicates = None

        if not offline_check:
            # Validate keyword argument names by querying valid predicates from
            # Space-Track
            predicates = self.get_predicates(class_, controller)
            predicate_fields = {p.name for p in predicates}
            valid_fields |= predicate_fields
        else:
            valid_fields |= self.offline_predicates[(class_, controller)]

        for key, value in kwargs.items():
            if key not in valid_fields:
                raise TypeError(
                    "'{class_}' got an unexpected argument '{key}'"
                    .format(class_=class_, key=key))

            if class_ == 'upload' and key == 'file':
                continue

            value = _stringify_predicate_value(value)

            url += '/{key}/{value}'.format(key=key, value=value)

        logger.debug(requests.utils.requote_uri(url))

        if class_ == 'upload':
            if 'file' not in kwargs:
                raise TypeError("missing keyword argument: 'file'")

            resp = self.session.post(url, files={'file': kwargs['file']})
        else:
            resp = self._ratelimited_get(url, stream=iter_lines or iter_content)

        _raise_for_status(resp)

        if resp.encoding is None:
            resp.encoding = 'UTF-8'

        if iter_lines:
            return _iter_lines_generator(resp, decode_unicode=decode)
        elif iter_content:
            return _iter_content_generator(resp, decode_unicode=decode)
        else:
            # If format is specified, return that format unparsed. Otherwise,
            # parse the default JSON response.
            if 'format' in kwargs:
                if decode:
                    data = resp.text
                    # Replace CRLF newlines with LF, Python will handle platform
                    # specific newlines if written to file.
                    data = data.replace('\r\n', '\n')
                else:
                    data = resp.content
                return data
            else:
                data = resp.json()

                if predicates is None or not parse_types:
                    return data
                else:
                    return self._parse_types(data, predicates)

Response: Generic request for the given object.


Instruction:
How would you explain what the following Python 3 function does
def _ratelimited_get(self, *args, **kwargs):
        with self._ratelimiter:
            resp = self.session.get(*args, **kwargs)

        # It's possible that Space-Track will return HTTP status 500 with a
        # query rate limit violation. This can happen if a script is cancelled
        # before it has finished sleeping to satisfy the rate limit and it is
        # started again.
        #
        # Let's catch this specific instance and retry once if it happens.
        if resp.status_code == 500:
            # Let's only retry if the error page tells us it's a rate limit
            # violation.
            if 'violated your query rate limit' in resp.text:
                # Mimic the RateLimiter callback behaviour.
                until = time.time() + self._ratelimiter.period
                t = threading.Thread(target=self._ratelimit_callback, args=(until,))
                t.daemon = True
                t.start()
                time.sleep(self._ratelimiter.period)

                # Now retry
                with self._ratelimiter:
                    resp = self.session.get(*args, **kwargs)

        return resp

Response: Perform a get request handling rate limiting.


Instruction:
Can you generate the documentation for the following Python 3 function
def _find_controller(self, class_):
        for controller, classes in self.request_controllers.items():
            if class_ in classes:
                return controller
        else:
            raise ValueError('Unknown request class {!r}'.format(class_))

Response: Find first controller that matches given request class.


Instruction:
Given the following Python 3 function, write the documentation
def _download_predicate_data(self, class_, controller):
        self.authenticate()

        url = ('{0}{1}/modeldef/class/{2}'
               .format(self.base_url, controller, class_))

        logger.debug(requests.utils.requote_uri(url))

        resp = self._ratelimited_get(url)

        _raise_for_status(resp)

        return resp.json()['data']

Response:
Get raw predicate information for given request class and cache for
        subsequent calls.


Instruction:
Given the following Python 3 function, write the documentation
def get_predicates(self, class_, controller=None):
        if class_ not in self._predicates:
            if controller is None:
                controller = self._find_controller(class_)
            else:
                classes = self.request_controllers.get(controller, None)
                if classes is None:
                    raise ValueError(
                        'Unknown request controller {!r}'.format(controller))
                if class_ not in classes:
                    raise ValueError(
                        'Unknown request class {!r}'.format(class_))

            predicates_data = self._download_predicate_data(class_, controller)
            predicate_objects = self._parse_predicates_data(predicates_data)
            self._predicates[class_] = predicate_objects

        return self._predicates[class_]

Response:
Get full predicate information for given request class and cache
        for subsequent calls.


Instruction:
Implement a Python 3 function for
adding the folding menu to the editor.

Response:
def on_install(self, editor):
        """
        Add the folding menu to the editor, on install.

        :param editor: editor instance on which the mode has been installed to.
        """
        super(FoldingPanel, self).on_install(editor)
        self.context_menu = QtWidgets.QMenu(_('Folding'), self.editor)
        action = self.action_collapse = QtWidgets.QAction(
            _('Collapse'), self.context_menu)
        action.setShortcut('Shift+-')
        action.triggered.connect(self._on_action_toggle)
        self.context_menu.addAction(action)
        action = self.action_expand = QtWidgets.QAction(_('Expand'),
                                                        self.context_menu)
        action.setShortcut('Shift++')
        action.triggered.connect(self._on_action_toggle)
        self.context_menu.addAction(action)
        self.context_menu.addSeparator()
        action = self.action_collapse_all = QtWidgets.QAction(
            _('Collapse all'), self.context_menu)
        action.setShortcut('Ctrl+Shift+-')
        action.triggered.connect(self._on_action_collapse_all_triggered)
        self.context_menu.addAction(action)
        action = self.action_expand_all = QtWidgets.QAction(
            _('Expand all'), self.context_menu)
        action.setShortcut('Ctrl+Shift++')
        action.triggered.connect(self._on_action_expand_all_triggered)
        self.context_menu.addAction(action)
        self.editor.add_menu(self.context_menu)


Instruction:
Given the following Python 3 function, write the documentation
def _draw_rect(self, rect, painter):
        c = self._custom_color
        if self._native:
            c = self.get_system_bck_color()
        grad = QtGui.QLinearGradient(rect.topLeft(),
                                     rect.topRight())
        if sys.platform == 'darwin':
            grad.setColorAt(0, c.lighter(100))
            grad.setColorAt(1, c.lighter(110))
            outline = c.darker(110)
        else:
            grad.setColorAt(0, c.lighter(110))
            grad.setColorAt(1, c.lighter(130))
            outline = c.darker(100)
        painter.fillRect(rect, grad)
        painter.setPen(QtGui.QPen(outline))
        painter.drawLine(rect.topLeft() +
                         QtCore.QPointF(1, 0),
                         rect.topRight() -
                         QtCore.QPointF(1, 0))
        painter.drawLine(rect.bottomLeft() +
                         QtCore.QPointF(1, 0),
                         rect.bottomRight() -
                         QtCore.QPointF(1, 0))
        painter.drawLine(rect.topRight() +
                         QtCore.QPointF(0, 1),
                         rect.bottomRight() -
                         QtCore.QPointF(0, 1))
        painter.drawLine(rect.topLeft() +
                         QtCore.QPointF(0, 1),
                         rect.bottomLeft() -
                         QtCore.QPointF(0, 1))

Response:
Draw the fold zone rectangle using the current style primitive color
            or foldIndicatorBackground.


Instruction:
Implement a Python 3 function for
getting a system color for drawing the fold scope background.

Response:
def get_system_bck_color():
        """
        Gets a system color for drawing the fold scope background.
        """
        def merged_colors(colorA, colorB, factor):
            maxFactor = 100
            colorA = QtGui.QColor(colorA)
            colorB = QtGui.QColor(colorB)
            tmp = colorA
            tmp.setRed((tmp.red() * factor) / maxFactor +
                       (colorB.red() * (maxFactor - factor)) / maxFactor)
            tmp.setGreen((tmp.green() * factor) / maxFactor +
                         (colorB.green() * (maxFactor - factor)) / maxFactor)
            tmp.setBlue((tmp.blue() * factor) / maxFactor +
                        (colorB.blue() * (maxFactor - factor)) / maxFactor)
            return tmp

        pal = QtWidgets.QApplication.instance().palette()
        b = pal.window().color()
        h = pal.highlight().color()
        return merged_colors(b, h, 50)


Instruction:
How would you explain what the following Python 3 function does
def _draw_fold_indicator(self, top, mouse_over, collapsed, painter):
        rect = QtCore.QRect(0, top, self.sizeHint().width(),
                            self.sizeHint().height())
        if self._native:
            if os.environ['QT_API'].lower() not in PYQT5_API:
                opt = QtGui.QStyleOptionViewItemV2()
            else:
                opt = QtWidgets.QStyleOptionViewItem()
            opt.rect = rect
            opt.state = (QtWidgets.QStyle.State_Active |
                         QtWidgets.QStyle.State_Item |
                         QtWidgets.QStyle.State_Children)
            if not collapsed:
                opt.state |= QtWidgets.QStyle.State_Open
            if mouse_over:
                opt.state |= (QtWidgets.QStyle.State_MouseOver |
                              QtWidgets.QStyle.State_Enabled |
                              QtWidgets.QStyle.State_Selected)
                opt.palette.setBrush(QtGui.QPalette.Window,
                                     self.palette().highlight())
            opt.rect.translate(-2, 0)
            self.style().drawPrimitive(QtWidgets.QStyle.PE_IndicatorBranch,
                                       opt, painter, self)
        else:
            index = 0
            if not collapsed:
                index = 2
            if mouse_over:
                index += 1
            QtGui.QIcon(self._custom_indicators[index]).paint(painter, rect)

Response: Draw the fold indicator.


Instruction:
How would you implement a function in Python 3 that
gets the base scope highlight color

Response:
def _get_scope_highlight_color(self):
        """
        Gets the base scope highlight color (derivated from the editor
        background)

        """
        color = self.editor.background
        if color.lightness() < 128:
            color = drift_color(color, 130)
        else:
            color = drift_color(color, 105)
        return color


Instruction:
Make a summary of the following Python 3 code
def _add_scope_deco(self, start, end, parent_start, parent_end, base_color,
                        factor):
        """
        Adds a scope decoration that enclose the current scope
        :param start: Start of the current scope
        :param end: End of the current scope
        :param parent_start: Start of the parent scope
        :param parent_end: End of the parent scope
        :param base_color: base color for scope decoration
        :param factor: color factor to apply on the base color (to make it
            darker).
        """
        color = drift_color(base_color, factor=factor)
        # upper part
        if start > 0:
            d = TextDecoration(self.editor.document(),
                               start_line=parent_start, end_line=start)
            d.set_full_width(True, clear=False)
            d.draw_order = 2
            d.set_background(color)
            self.editor.decorations.append(d)
            self._scope_decos.append(d)
        # lower part
        if end <= self.editor.document().blockCount():
            d = TextDecoration(self.editor.document(),
                               start_line=end, end_line=parent_end + 1)
            d.set_full_width(True, clear=False)
            d.draw_order = 2
            d.set_background(color)
            self.editor.decorations.append(d)
            self._scope_decos.append(d)

Response: Adds a scope decoration that enclose the current scope.


Instruction:
How would you explain what the following Python 3 function does
def _add_scope_decorations(self, block, start, end):
        try:
            parent = FoldScope(block).parent()
        except ValueError:
            parent = None
        if TextBlockHelper.is_fold_trigger(block):
            base_color = self._get_scope_highlight_color()
            factor_step = 5
            if base_color.lightness() < 128:
                factor_step = 10
                factor = 70
            else:
                factor = 100
            while parent:
                # highlight parent scope
                parent_start, parent_end = parent.get_range()
                self._add_scope_deco(
                    start, end + 1, parent_start, parent_end,
                    base_color, factor)
                # next parent scope
                start = parent_start
                end = parent_end
                parent = parent.parent()
                factor += factor_step
            # global scope
            parent_start = 0
            parent_end = self.editor.document().blockCount()
            self._add_scope_deco(
                start, end + 1, parent_start, parent_end, base_color,
                factor + factor_step)
        else:
            self._clear_scope_decos()

Response: Add scope decoration on the editor widget.


Instruction:
How would you implement a function in Python 3 that
highlights the scopes surrounding the current fold scope.

Response:
def _highlight_surrounding_scopes(self, block):
        """
        Highlights the scopes surrounding the current fold scope.

        :param block: Block that starts the current fold scope.
        """
        scope = FoldScope(block)
        if (self._current_scope is None or
                self._current_scope.get_range() != scope.get_range()):
            self._current_scope = scope
            self._clear_scope_decos()
            # highlight surrounding parent scopes with a darker color
            start, end = scope.get_range()
            if not TextBlockHelper.is_collapsed(block):
                self._add_scope_decorations(block, start, end)


Instruction:
Can you write a function in Python 3 where it
removes scope decorations and background from the editor and the panel.

Response:
def leaveEvent(self, event):
        """
        Removes scope decorations and background from the editor and the panel
        if highlight_caret_scope, else simply update the scope decorations to
        match the caret scope.

        """
        super(FoldingPanel, self).leaveEvent(event)
        QtWidgets.QApplication.restoreOverrideCursor()
        self._highlight_runner.cancel_requests()
        if not self.highlight_caret_scope:
            self._clear_scope_decos()
            self._mouse_over_line = None
            self._current_scope = None
        else:
            self._block_nbr = -1
            self._highlight_caret_scope()
        self.editor.repaint()


Instruction:
How would you explain what the following Python 3 function does
def _add_fold_decoration(self, block, region):
        deco = TextDecoration(block)
        deco.signals.clicked.connect(self._on_fold_deco_clicked)
        deco.tooltip = region.text(max_lines=25)
        deco.draw_order = 1
        deco.block = block
        deco.select_line()
        deco.set_outline(drift_color(
            self._get_scope_highlight_color(), 110))
        deco.set_background(self._get_scope_highlight_color())
        deco.set_foreground(QtGui.QColor('#808080'))
        self._block_decos.append(deco)
        self.editor.decorations.append(deco)

Response: Add fold decorations to the editor.


Instruction:
Can you tell what is the following Python 3 function doing
def toggle_fold_trigger(self, block):
        if not TextBlockHelper.is_fold_trigger(block):
            return
        region = FoldScope(block)
        if region.collapsed:
            region.unfold()
            if self._mouse_over_line is not None:
                self._add_scope_decorations(
                    region._trigger, *region.get_range())
        else:
            region.fold()
            self._clear_scope_decos()
        self._refresh_editor_and_scrollbars()
        self.trigger_state_changed.emit(region._trigger, region.collapsed)

Response: Toggle a fold trigger block.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def on_state_changed(self, state):
        if state:
            self.editor.key_pressed.connect(self._on_key_pressed)
            if self._highlight_caret:
                self.editor.cursorPositionChanged.connect(
                    self._highlight_caret_scope)
                self._block_nbr = -1
            self.editor.new_text_set.connect(self._clear_block_deco)
        else:
            self.editor.key_pressed.disconnect(self._on_key_pressed)
            if self._highlight_caret:
                self.editor.cursorPositionChanged.disconnect(
                    self._highlight_caret_scope)
                self._block_nbr = -1
            self.editor.new_text_set.disconnect(self._clear_block_deco)

Response: Connect to the cursorPositionChanged signal and disconnect from the new_text_set signal


Instruction:
Create a Python 3 function to
override key press to select the current scope if the user wants to delete a folded scope.

Response:
def _on_key_pressed(self, event):
        """
        Override key press to select the current scope if the user wants
        to deleted a folded scope (without selecting it).
        """
        delete_request = event.key() in [QtCore.Qt.Key_Backspace,
                                         QtCore.Qt.Key_Delete]
        if event.text() or delete_request:
            cursor = self.editor.textCursor()
            if cursor.hasSelection():
                # change selection to encompass the whole scope.
                positions_to_check = cursor.selectionStart(), cursor.selectionEnd()
            else:
                positions_to_check = (cursor.position(), )
            for pos in positions_to_check:
                block = self.editor.document().findBlock(pos)
                th = TextBlockHelper()
                if th.is_fold_trigger(block) and th.is_collapsed(block):
                    self.toggle_fold_trigger(block)
                    if delete_request and cursor.hasSelection():
                        scope = FoldScope(self.find_parent_scope(block))
                        tc = TextHelper(self.editor).select_lines(*scope.get_range())
                        if tc.selectionStart() > cursor.selectionStart():
                            start = cursor.selectionStart()
                        else:
                            start = tc.selectionStart()
                        if tc.selectionEnd() < cursor.selectionEnd():
                            end = cursor.selectionEnd()
                        else:
                            end = tc.selectionEnd()
                        tc.setPosition(start)
                        tc.setPosition(end, tc.KeepAnchor)
                        self.editor.setTextCursor(tc)


Instruction:
Can you generate a brief explanation for the following Python 3 code
def verify_signature(self, payload, headers):
        github_signature = headers.get("x-hub-signature")
        if not github_signature:
            json_abort(401, "X-Hub-Signature header missing.")

        gh_webhook_secret = current_app.config["GITHUB_WEBHOOK_SECRET"]
        signature = "sha1={}".format(
            hmac.new(
                gh_webhook_secret.encode("utf-8"), payload, hashlib.sha1
            ).hexdigest()
        )

        if not hmac.compare_digest(signature, github_signature):
            json_abort(401, "Request signature does not match calculated signature.")

Response: Verify that the payload was sent from GitHub instance.


Instruction:
Can you write a function in Python 3 where it
gets the options that have been set.

Response:
def get_options(self):
        """Get the options that have been set.

        Called after the user has added all their own options
        and is ready to use the variables.

        """
        (options, args) = self.parser.parse_args()

        # Set values from .visdkrc, but only if they haven't already been set
        visdkrc_opts = self.read_visdkrc()
        for opt in self.config_vars:
            if not getattr(options, opt):
                # Try and use value from visdkrc
                if visdkrc_opts:
                    if opt in visdkrc_opts:
                        setattr(options, opt, visdkrc_opts[opt])

        # Ensure all the required options are set
        for opt in self.required_opts:
            if opt not in dir(options) or getattr(options, opt) == None:
                self.parser.error('%s must be set!' % opt)

        return options


Instruction:
Make a summary of the following Python 3 code
def parse_job_files(self):
        repo_jobs = []
        for rel_job_file_path, job_info in self.job_files.items():
            LOGGER.debug("Checking for job definitions in %s", rel_job_file_path)
            jobs = self.parse_job_definitions(rel_job_file_path, job_info)
            LOGGER.debug("Found %d job definitions in %s", len(jobs), rel_job_file_path)
            repo_jobs.extend(jobs)
        if not repo_jobs:
            LOGGER.info("No job definitions found in repo '%s'", self.repo)
        else:
            LOGGER.info(
                "Found %d job definitions in repo '%s'", len(repo_jobs), self.repo
            )
            # LOGGER.debug(json.dumps(repo_jobs, indent=4))
        return repo_jobs

Response: Check for job definitions in known zuul files.


Instruction:
Can you tell what is the following Python 3 function doing
def change_directory(self, directory):
        self._process.write(('cd %s\n' % directory).encode())
        if sys.platform == 'win32':
            self._process.write((os.path.splitdrive(directory)[0] + '\r\n').encode())
            self.clear()
        else:
            self._process.write(b'\x0C')

Response: Changes the current directory.


Instruction:
How would you explain what the following Python 3 function does
def icon(self):
        if isinstance(self._icon, str):
            if QtGui.QIcon.hasThemeIcon(self._icon):
                return QtGui.QIcon.fromTheme(self._icon)
            else:
                return QtGui.QIcon(self._icon)
        elif isinstance(self._icon, tuple):
            return QtGui.QIcon.fromTheme(self._icon[0],
                                         QtGui.QIcon(self._icon[1]))
        elif isinstance(self._icon, QtGui.QIcon):
            return self._icon
        return QtGui.QIcon()

Response: Gets the icon file name. Read - only.


Instruction:
Given the following Python 3 function, write the documentation
def add_marker(self, marker):
        self._markers.append(marker)
        doc = self.editor.document()
        assert isinstance(doc, QtGui.QTextDocument)
        block = doc.findBlockByLineNumber(marker._position)
        marker.block = block
        d = TextDecoration(block)
        d.set_full_width()
        if self._background:
            d.set_background(QtGui.QBrush(self._background))
            marker.decoration = d
        self.editor.decorations.append(d)
        self.repaint()

Response: Adds the marker to the panel.


Instruction:
How would you explain what the following Python 3 function does
def remove_marker(self, marker):
        self._markers.remove(marker)
        self._to_remove.append(marker)
        if hasattr(marker, 'decoration'):
            self.editor.decorations.remove(marker.decoration)
        self.repaint()

Response: Removes a marker from the panel


Instruction:
Can you generate a brief explanation for the following Python 3 code
def marker_for_line(self, line):
        markers = []
        for marker in self._markers:
            if line == marker.position:
                markers.append(marker)
        return markers

Response: Returns the marker that is displayed at the specified line number if any.


Instruction:
Implement a function in Python 3 to
display the given tooltip at the specified top position.

Response:
def _display_tooltip(self, tooltip, top):
        """
        Display tooltip at the specified top position.
        """
        QtWidgets.QToolTip.showText(self.mapToGlobal(QtCore.QPoint(
            self.sizeHint().width(), top)), tooltip, self)


Instruction:
Can you generate a brief explanation for the following Python 3 code
def finditer_noregex(string, sub, whole_word):
    start = 0
    while True:
        start = string.find(sub, start)
        if start == -1:
            return
        if whole_word:
            if start:
                pchar = string[start - 1]
            else:
                pchar = ' '
            try:
                nchar = string[start + len(sub)]
            except IndexError:
                nchar = ' '
            if nchar in DocumentWordsProvider.separators and \
                    pchar in DocumentWordsProvider.separators:
                yield start
            start += len(sub)
        else:
            yield start
            start += 1

Response: Search occurrences using str. find instead of regular expressions.