Instruction:
Write a Python 3 function for
moving to given step

Response:
def move_to_step(self, step):
        """
        Use in cases when you need to move in given step depending on input
        """
        if step not in self._scenario_steps.keys():
            raise UndefinedState("step {} not defined in scenario".format(step))
        try:
            session_id = session.sessionId
            self.session_machines.set_state(session_id, step)
        except UninitializedStateMachine as e:
            logger.error(e)
            return statement(INTERNAL_ERROR_MSG)


Instruction:
How would you implement a function in Python 3 that
gets current state of user session or None if session doesn t exist.

Response:
def get_current_state(self):
        """
        Get current state for user session or None if session doesn't exist
        """
        try:
            session_id = session.sessionId
            return self.session_machines.current_state(session_id)
        except UninitializedStateMachine as e:
            logger.error(e)


Instruction:
Explain what the following Python 3 code does
def get_help(self):
        current_state = self.get_current_state()
        if current_state is None:
            return statement(INTERNAL_ERROR_MSG)
        else:
            try:
                return choice(self._scenario_steps[current_state]['help'])
            except KeyError:
                return choice(self._default_help)

Response: Get context help depending on the current step.


Instruction:
Given the following Python 3 function, write the documentation
def run(self):
        '''
        Runs all enabled tests.
        '''
        # Run all tests.
        for cls in self.get_test_classes():

            # Print informational message.
            self.logger.info('Running {cls.__name__} test...'.format(cls=cls))

            # Create new test instance.
            test = cls(runner=self)

            # Run test and evaluate result.
            if test._run():
                self.logger.passed('Test {cls.__name__} succeeded!'.format(cls=cls))
            else:
                self.logger.failed('Test {cls.__name__} failed!'.format(cls=cls))
                self.has_passed = False

        # Print summary.
        if self.has_passed:
            self.logger.passed('Summary: All tests passed!')
        else:
            self.logger.failed('Summary: One or more tests failed!')

        return self.has_passed

Response: Runs all enabled tests and evaluates result.


Instruction:
Can you write a function in Python 3 where it
raises an exception if the executable is not found in the PATH.

Response:
def check_executable(tool_name):
    """Raises an warning if the executable *tool_name* is not found."""
    result = subprocess.call(['which', tool_name], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    if result != 0:
        message = "The executable '%s' cannot be found in your $PATH" % tool_name
        raise Exception(message)


Instruction:
Write a Python 3 function that can
call sys. exit if the module *mod_name* is not found.

Response:
def check_module(mod_name):
    """Calls sys.exit() if the module *mod_name* is not found."""
    # Special cases #
    if mod_name in module_name: mod_name = module_name[mod_name]
    # Use a try except block #
    try:
        __import__(mod_name)
    except ImportError as e:
        if str(e) != 'No module named %s' % mod_name: raise e
        print 'You do not seem to have the "%s" package properly installed.' \
              ' Either you never installed it or your $PYTHONPATH is not set up correctly.' \
              ' For more instructions see the README file. (%s)' % (mod_name, e)
        sys.exit()


Instruction:
How would you implement a function in Python 3 that
generates the IIIF image key.

Response:
def iiif_image_key(obj):
    """Generate the IIIF image key."""
    if isinstance(obj, ObjectVersion):
        bucket_id = obj.bucket_id
        version_id = obj.version_id
        key = obj.key
    else:
        bucket_id = obj.get('bucket')
        version_id = obj.get('version_id')
        key = obj.get('key')
    return u'{}:{}:{}'.format(
        bucket_id,
        version_id,
        key,
    )


Instruction:
Create a Python 3 function to
generate IIIF image URL from the UI application.

Response:
def ui_iiif_image_url(obj, version='v2', region='full', size='full',
                      rotation=0, quality='default', image_format='png'):
    """Generate IIIF image URL from the UI application."""
    return u'{prefix}{version}/{identifier}/{region}/{size}/{rotation}/' \
        u'{quality}.{image_format}'.format(
            prefix=current_app.config['IIIF_UI_URL'],
            version=version,
            identifier=quote(
                iiif_image_key(obj).encode('utf8'), safe=':'),
            region=region,
            size=size,
            rotation=rotation,
            quality=quality,
            image_format=image_format,
        )


Instruction:
Create a Python 3 function for
returning True if the given s - expression matches the current non - terminal.

Response:
def matches(self, sexp):
        '''
        Body of a non-terminal is always a :class:`Sequence`. For an s-expr
        to match, it must be of the form::

            ['name'] + [sexpr-0, ..., sexpr-n]

        where the first list contains a name of the non-terminal,
        and the second one matches its body sequence.
        '''
        if sexp and isinstance(sexp, list) and self.name == sexp[0]:
            return self.body.matches(sexp[1:])
        return False


Instruction:
Create a Python 3 function for
rendering appropriate template with embed flag.

Response:
def preview(file):
    """Render appropriate template with embed flag."""
    params = deepcopy(current_app.config['IIIF_PREVIEWER_PARAMS'])
    if 'image_format' not in params:
        params['image_format'] = \
            'png' if file.has_extensions('.png') else 'jpg'
    return render_template(
        current_app.config['IIIF_PREVIEW_TEMPLATE'],
        file=file,
        file_url=ui_iiif_image_url(
            file.file,
            **params
        )
    )


Instruction:
Can you tell what is the following Python 3 function doing
def get_translated_attribute(instance, attr):

	# If its class has no translatable fields, returns attribute
	try:
		if not hasattr(instance._meta, "translatable_fields") or len(getattr(instance._meta,"translatable_fields"))==0:
			return getattr(instance, attr)
	except AttributeError:
		return instance

	# Translatable fields of this instance
	translatable_fields = instance._meta.translatable_fields

	# Current language
	cur_language = get_language()
	lang = cur_language.title().lower()

	# If current language is default language, returns attribute
	if lang == settings.LANGUAGE_CODE:
		return getattr(instance, attr)

	# Otherwise, if a translation is NOT needed for attr atribute, get attribute
	if not attr in translatable_fields:
		return getattr(instance, attr)

	# Gets field translations of this instance and return the translated attribute
	field_translation = _get_fieldtranslations(instance, field=attr, lang=lang)
	if field_translation:
		if not field_translation.is_fuzzy:
			return field_translation.translation
	return getattr(instance, attr)

Response: Gets the translated attribute of the given instance.


Instruction:
Given the following Python 3 function, write the documentation
def secure(view):
    AUTH = getattr(
        settings,
        'SLACKCHAT_AUTH_DECORATOR',
        'django.contrib.admin.views.decorators.staff_member_required'
    )
    auth_decorator = import_class(AUTH)
    return method_decorator(auth_decorator, name='dispatch')(view)

Response: Set an auth decorator applied for views.


Instruction:
Given the following Python 3 function, write the documentation
def import_translations(request, language):
	def _import_po_file(uploadedfile, lang):
		lines = []
		for line in uploadedfile:
			lines.append(line)
		num_lines = len(lines)

		prog_ctxt = re.compile(r"msgctxt\s+\"(?P<id>\d+)--(?P<model>\w+)--(?P<object_id>\d+)--(?P<field>\w+)\"")
		prog_msgid = re.compile(r"msgid\s+\"(?P<source_text>.+)\"$")
		prog_msgstr = re.compile(r"msgstr\s+(?P<trans>.+)")

		i = 0
		while i < num_lines:
			line = lines[i]
			result = prog_ctxt.match(line)
			if result:
				id = result.group("id")
				is_fuzzy = (lines[i-1] == "#, fuzzy\n")

				source_text = lines[i+1]
				translation_line = lines[i+2]

				# Traducción
				g = prog_msgstr.match(translation_line)
				if g is None:
					i += 1
					continue
				translation = g.group("trans").replace("msgstr","")[1:-1].replace("\\\"","\"").replace('\\\'','\'')

				# Obtención de la traducción a partir del id
				try:
					field_trans = FieldTranslation.objects.get(id=id)
				except FieldTranslation.DoesNotExist:
					source_text = source_text.replace("msgid","")[1:-1].replace("\\\"","\"").replace('\\\'','\'')
					source_md5 = hashlib.md5(source_text.encode("utf-8")).hexdigest()
					field_trans = FieldTranslation(model=result.group("model"), object_id=result.group("object_id"), field=result.group("field"), lang=lang, source_text=source_text, source_md5=source_md5)

				# Establecemos la traducción y si es fuzzy
				field_trans.translation = translation
				field_trans.is_fuzzy = is_fuzzy
				field_trans.save()
				#print translation
				#print is_fuzzy
				i += 4
			i += 1

	# Elimina traducciones que no estén asociadas a ningún objeto
	FieldTranslation.delete_orphan_translations()

	# Acceso obligatoriamente por POST
	if request.method != "POST":
		return HttpResponseRedirect(reverse("modeltranslation:admin_url"))

	form = ImportTranslationsForm(request.POST, request.FILES)
	if form.is_valid():
		_import_po_file(request.FILES['file'], language)
		#cache = TransCache.factory()
		#cache.clear()
		return HttpResponseRedirect(reverse("modeltranslation:view_all_url",args=(language,"all")))

	return HttpResponseRedirect(reverse("modeltranslation:admin_url"))

Response: Import translations from a file.


Instruction:
Given the following Python 3 function, write the documentation
def up(cloud_init, use_snapshots, upgrade_image, snapshot_cluster, snapshot_time):
    try:
        cloud_config = CloudConfig()
        ci = None

        if cloud_init:
            ci = CloudInit()

        cloud_controller = CloudController(cloud_config)
        cloud_controller.up(ci, use_snapshots, upgrade_image, snapshot_cluster, snapshot_time)
    except CloudComposeException as ex:
        print(ex)

Response: Creates a new cluster and creates a new one


Instruction:
Create a Python 3 function to
destroy an existing cluster

Response:
def down(force):
    """
    destroys an existing cluster
    """
    try:
        cloud_config = CloudConfig()
        cloud_controller = CloudController(cloud_config)
        cloud_controller.down(force)
    except CloudComposeException as ex:
        print(ex)


Instruction:
Write a Python 3 function that can
delete all launch configs and auto scaling group

Response:
def cleanup():
    """
    deletes launch configs and auto scaling group
    """
    try:
        cloud_config = CloudConfig()
        cloud_controller = CloudController(cloud_config)
        cloud_controller.cleanup()
    except CloudComposeException as ex:
        print(ex)


Instruction:
Can you generate a brief explanation for the following Python 3 code
def build():
    try:
        cloud_config = CloudConfig()
        config_data = cloud_config.config_data('cluster')
        cloud_init = CloudInit()
        print(cloud_init.build(config_data))
    except CloudComposeException as ex:
        print(ex)

Response: builds the cloud_init script


Instruction:
Can you tell what is the following Python 3 function doing
def flush(signal_names, exclude, wait):

    signalbus = current_app.extensions['signalbus']
    signal_names = set(signal_names)
    exclude = set(exclude)
    models_to_flush = signalbus.get_signal_models()
    if signal_names and exclude:
        click.echo('Warning: Specified both SIGNAL_NAMES and exclude option.')
    if signal_names:
        wrong_signal_names = signal_names - {m.__name__ for m in models_to_flush}
        models_to_flush = [m for m in models_to_flush if m.__name__ in signal_names]
    else:
        wrong_signal_names = exclude - {m.__name__ for m in models_to_flush}
    for name in wrong_signal_names:
        click.echo('Warning: A signal with name "{}" does not exist.'.format(name))
    models_to_flush = [m for m in models_to_flush if m.__name__ not in exclude]
    logger = logging.getLogger(__name__)
    try:
        if wait is not None:
            signal_count = signalbus.flush(models_to_flush, wait=max(0.0, wait))
        else:
            signal_count = signalbus.flush(models_to_flush)
    except Exception:
        logger.exception('Caught error while sending pending signals.')
        sys.exit(1)
    if signal_count == 1:
        logger.warning('%i signal has been successfully processed.', signal_count)
    elif signal_count > 1:
        logger.warning('%i signals have been successfully processed.', signal_count)

Response: Send pending signals over the message bus.


Instruction:
Can you tell what is the following Python 3 function doing
def flushmany():

    signalbus = current_app.extensions['signalbus']
    signal_count = signalbus.flushmany()
    logger = logging.getLogger(__name__)
    if signal_count == 1:
        logger.warning('%i signal has been successfully processed.', signal_count)
    elif signal_count > 1:
        logger.warning('%i signals have been successfully processed.', signal_count)

Response: Flushes a potentially huge number of pending signals over the message bus.


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

    signalbus = current_app.extensions['signalbus']
    for signal_model in signalbus.get_signal_models():
        click.echo(signal_model.__name__)

Response: Show all signal types.


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

    signalbus = current_app.extensions['signalbus']
    pending = []
    total_pending = 0
    for signal_model in signalbus.get_signal_models():
        count = signal_model.query.count()
        if count > 0:
            pending.append((count, signal_model.__name__))
        total_pending += count
    if pending:
        pending.sort()
        max_chars = len(str(pending[-1][0]))
        for n, signal_name in pending:
            click.echo('{} of type "{}"'.format(str(n).rjust(max_chars), signal_name))
    click.echo(25 * '-')
    click.echo('Total pending: {} '.format(total_pending))

Response: Show the number of pending signals by signal type.


Instruction:
Can you tell what is the following Python 3 function doing
def _create_key(lang, instance):
        model_name = instance.__class__.__name__
        return "{0}__{1}_{2}".format(lang,model_name,instance.id)

Response: Crea la clave única de la caché


Instruction:
Create a Python 3 function to
set the language of the given instance in the cache.

Response:
def set(self, lang, instance):
        """
        Establece en la instancia actual los atributos de traducción
        y la almacena en un diccionario de claves _create_key y valores
        el objeto con los atributos dinámicos.
        """
        if self._cache_is_too_big():
            self.cache = {}
        instance_key = TransCache._create_key(lang, instance)
        instance._translations_are_cached = True
        instance.load_translations(lang=lang)
        self.cache[instance_key] = instance


Instruction:
Can you tell what is the following Python 3 function doing
def get(self, lang, instance):
        instance_key = TransCache._create_key(lang, instance)
        return self.cache[instance_key]

Response: Get a cache entry for a given language and instance.


Instruction:
Explain what the following Python 3 code does
def create_thumbnail(uuid, thumbnail_width):
    # size = '!' + thumbnail_width + ','
    size = thumbnail_width + ','  # flask_iiif doesn't support ! at the moment
    thumbnail = IIIFImageAPI.get('v2', uuid, size, 0, 'default', 'jpg')

Response: Create the thumbnail for an image.


Instruction:
How would you implement a function in Python 3 that
converts raw price value into a string that can be used as a price value in any localization .

Response:
def price_str(raw_price, default=_not_defined, dec_point='.'):
    """Search and clean price value.

    Convert raw price string presented in any localization
    as a valid number string with an optional decimal point.

    If raw price does not contain valid price value or contains
    more than one price value, then return default value.
    If default value not set, then raise ValueError.

    Examples:
        12.007          => 12007
        00012,33        => 12.33
        +1              => 1
        - 520.05        => -520.05
        1,000,777.5     => 1000777.5
        1.777.000,99    => 1777000.99
        1 234 567.89    => 1234567.89
        99.77.11.000,1  => 997711000.1
        NIO5,242        => 5242
        Not a MINUS-.45 => 45
          42  \t \n     => 42
                        => <default>
        1...2           => <default>

    :param str raw_price: string that contains price value.
    :param default: value that will be returned if raw price not valid.
    :param str dec_point: symbol that separate integer and fractional parts.
    :return: cleaned price string.
    :raise ValueError: error if raw price not valid and default value not set.
    """
    def _error_or_default(err_msg):
        if default == _not_defined:
            raise ValueError(err_msg)
        return default

    # check and clean
    if not isinstance(raw_price, str):
        return _error_or_default(
            'Wrong raw price type "{price_type}" '
            '(expected type "str")'.format(price_type=type(raw_price)))

    price = re.sub('\s', '', raw_price)
    cleaned_price = _CLEANED_PRICE_RE.findall(price)

    if len(cleaned_price) == 0:
        return _error_or_default(
            'Raw price value "{price}" does not contain '
            'valid price digits'.format(price=raw_price))

    if len(cleaned_price) > 1:
        return _error_or_default(
            'Raw price value "{price}" contains '
            'more than one price value'.format(price=raw_price))

    price = cleaned_price[0]

    # clean truncated decimal (e.g. 99. -> 99)
    price = price.rstrip('.,')

    # get sign
    sign = ''
    if price[0] in {'-', '+'}:
        sign, price = price[0], price[1:]
        sign = '-' if sign == '-' else ''

    # extract fractional digits
    fractional = _FRACTIONAL_PRICE_RE.match(price)
    if fractional:
        integer, fraction = fractional.groups()
    else:
        integer, fraction = price, ''

    # leave only digits in the integer part of the price
    integer = re.sub('\D', '', integer)

    # remove leading zeros (e.g. 007 -> 7, but 0.1 -> 0.1)
    integer = integer.lstrip('0')
    if integer == '':
        integer = '0'

    # construct price
    price = sign + integer
    if fraction:
        price = ''.join((price, dec_point, fraction))

    return price


Instruction:
Write a Python 3 script for
returning Decimal price value from raw string.

Response:
def price_dec(raw_price, default=_not_defined):
    """Price decimal value from raw string.

    Extract price value from input raw string and
    present as Decimal number.

    If raw price does not contain valid price value or contains
    more than one price value, then return default value.
    If default value not set, then raise ValueError.

    :param str raw_price: string that contains price value.
    :param default: value that will be returned if raw price not valid.
    :return: Decimal price value.
    :raise ValueError: error if raw price not valid and default value not set.
    """
    try:
        price = price_str(raw_price)
        return decimal.Decimal(price)

    except ValueError as err:
        if default == _not_defined:
            raise err

    return default


Instruction:
How would you explain what the following Python 3 function does
def flotify(result, num=50):
    results = list(TestResult.objects.filter(test=result.test, site=result.site)[:num])
    results.reverse()
    return [[get_timestamp(result.run_date), result.duration/1000] for result in results]

Response: Flotify a test result.


Instruction:
Implement a function in Python 3 to
get something from self which could be an attribute or a callable with either 0 or 1 arguments.

Response:
def __get_dynamic_attr(self, attname, arg, default=None):
        """
        Gets "something" from self, which could be an attribute or
        a callable with either 0 or 1 arguments (besides self).

        Stolen from django.contrib.syntication.feeds.Feed.
        """
        try:
            attr = getattr(self, attname)
        except AttributeError:
            return default
        if callable(attr):
            # Check func_code.co_argcount rather than try/excepting the
            # function and catching the TypeError, because something inside
            # the function may raise the TypeError. This technique is more
            # accurate.
            if hasattr(attr, 'func_code'):
                argcount = attr.func_code.co_argcount
            else:
                argcount = attr.__call__.func_code.co_argcount
            if argcount == 2: # one argument is 'self'
                return attr(arg)
            else:
                return attr()
        return attr


Instruction:
Can you implement a function in Python 3 that
appends global tags configured for the client to the tags given then converts the data into InfluxDB Line protocol and sends to to socket

Response:
def metric(self, measurement_name, values, tags=None, timestamp=None):
        """
        Append global tags configured for the client to the tags given then
        converts the data into InfluxDB Line protocol and sends to to socket
        """
        if not measurement_name or values in (None, {}):
            # Don't try to send empty data
            return

        tags = tags or {}

        # Do a shallow merge of the metric tags and global tags
        all_tags = dict(self.tags, **tags)

        # Create a metric line from the input and then send it to socket
        line = Line(measurement_name, values, all_tags, timestamp)
        self.send(line.to_line_protocol())


Instruction:
How would you implement a function in Python 3 that
sends the given data to the socket via UDP.

Response:
def send(self, data):
        """
        Sends the given data to the socket via UDP
        """
        try:
            self.socket.sendto(data.encode('utf8') + b'\n', (self.host, self.port))
        except (socket.error, RuntimeError):
            # Socket errors should fail silently so they don't affect anything else
            pass


Instruction:
Given the following Python 3 function, write the documentation
def send(self, data):
        self.future_session.post(url=self.url, data=data)

Response: Send the data in a separate thread via HTTP POST.


Instruction:
Can you tell what is the following Python 3 function doing
def get_output_values(self):
        # Handle primitive values here and implicitly convert them to a dict because
        # it allows the API to be simpler.

        # Also influxDB mandates that each value also has a name so the default name
        # for any non-dict value is "value"
        if not isinstance(self.values, dict):
            metric_values = {'value': self.values}
        else:
            metric_values = self.values

        # Sort the values in lexicographically by value name
        sorted_values = sorted(metric_values.items())

        # Remove None values
        sorted_values = [(k, v) for k, v in sorted_values if v is not None]

        return u",".join(u"{0}={1}".format(format_string(k), format_value(v)) for k, v in sorted_values)

Response: Return an escaped string of comma separated value_name = value pairs


Instruction:
Implement a Python 3 function for
returning an escaped string of comma separated tag_name = tag_value pairs

Response:
def get_output_tags(self):
        """
        Return an escaped string of comma separated tag_name: tag_value pairs

        Tags should be sorted by key before being sent for best performance. The sort should
        match that from the Go bytes. Compare function (http://golang.org/pkg/bytes/#Compare).
        """

        # Sort the tags in lexicographically by tag name
        sorted_tags = sorted(self.tags.items())

        # Finally render, escape and return the tag string
        return u",".join(u"{0}={1}".format(format_string(k), format_string(v)) for k, v in sorted_tags)


Instruction:
Given the following Python 3 function, write the documentation
def to_line_protocol(self):
        tags = self.get_output_tags()

        return u"{0}{1} {2}{3}".format(
            self.get_output_measurement(),
            "," + tags if tags else '',
            self.get_output_values(),
            self.get_output_timestamp()
        )

Response: Converts the given metrics as a single line of InfluxDB line protocol.


Instruction:
Write a Python 3 function that can
format a string for use in a log file.

Response:
def format_string(key):
    """
    Formats either measurement names, tag names or tag values.

    Measurement name and any optional tags separated by commas. Measurement names, tag keys,
    and tag values must escape any spaces, commas or equal signs using a backslash (\).
    For example: \ and \,.

    All tag values are stored as strings and should not be surrounded in quotes.
    """
    if isinstance(key, basestring):
        key = key.replace(",", "\,")
        key = key.replace(" ", "\ ")
        key = key.replace("=", "\=")
    return key


Instruction:
Make a summary of the following Python 3 code
def format_value(value):
    if isinstance(value, basestring):
        value = value.replace('"', '\"')
        value = u'"{0}"'.format(value)
    elif isinstance(value, bool):
        value = str(value)
    elif isinstance(value, int):
        value = "{0}i".format(value)
    elif isinstance(value, float):
        value = str(value)
    return value

Response: Format the value for the log entry.


Instruction:
Write a Python 3 function for
processing the equation and returns a dictionary containing eq properties.

Response:
def _process_equation(value, fmt):
    """Processes the equation.  Returns a dict containing eq properties."""

    # pylint: disable=global-statement
    global Nreferences  # Global references counter
    global cursec       # Current section

    # Parse the equation
    attrs = value[0]

    # Initialize the return value
    eq = {'is_unnumbered': False,
          'is_unreferenceable': False,
          'is_tagged': False,
          'attrs': attrs}

    # Bail out if the label does not conform
    if not LABEL_PATTERN.match(attrs[0]):
        eq['is_unnumbered'] = True
        eq['is_unreferenceable'] = True
        return eq

    # Process unreferenceable equations
    if attrs[0] == 'eq:': # Make up a unique description
        attrs[0] = attrs[0] + str(uuid.uuid4())
        eq['is_unreferenceable'] = True
        unreferenceable.append(attrs[0])

    # For html, hard-code in the section numbers as tags
    kvs = PandocAttributes(attrs, 'pandoc').kvs
    if numbersections and fmt in ['html', 'html5'] and 'tag' not in kvs:
        if kvs['secno'] != cursec:
            cursec = kvs['secno']
            Nreferences = 1
        kvs['tag'] = cursec + '.' + str(Nreferences)
        Nreferences += 1

    # Save to the global references tracker
    eq['is_tagged'] = 'tag' in kvs
    if eq['is_tagged']:
        # Remove any surrounding quotes
        if kvs['tag'][0] == '"' and kvs['tag'][-1] == '"':
            kvs['tag'] = kvs['tag'].strip('"')
        elif kvs['tag'][0] == "'" and kvs['tag'][-1] == "'":
            kvs['tag'] = kvs['tag'].strip("'")
        references[attrs[0]] = kvs['tag']
    else:
        Nreferences += 1
        references[attrs[0]] = Nreferences

    # Adjust equation depending on the output format
    if fmt in ['latex', 'beamer']:
        if not eq['is_unreferenceable']:  # Code in the tags
            value[-1] += r'\tag{%s}\label{%s}' % \
              (references[attrs[0]].replace(' ', r'\ '), attrs[0]) \
              if eq['is_tagged'] else r'\label{%s}'%attrs[0]
    elif fmt in ('html', 'html5'):
        pass  # Insert html in process_equations() instead
    else:  # Hard-code in the number/tag
        if isinstance(references[attrs[0]], int):  # Numbered reference
            value[-1] += r'\qquad (%d)' % references[attrs[0]]
        else:  # Tagged reference
            assert isinstance(references[attrs[0]], STRTYPES)
            text = references[attrs[0]].replace(' ', r'\ ')
            if text.startswith('$') and text.endswith('$'):  # Math
                tag = text[1:-1]
            else:  # Text
                tag = r'\text{%s}' % text
            value[-1] += r'\qquad (%s)' % tag

    return eq


Instruction:
Make a summary of the following Python 3 code
def process_equations(key, value, fmt, meta):

    if key == 'Math' and len(value) == 3:

        # Process the equation
        eq = _process_equation(value, fmt)

        # Get the attributes and label
        attrs = eq['attrs']
        label = attrs[0]
        if eq['is_unreferenceable']:
            attrs[0] = ''  # The label isn't needed outside this function

        # Context-dependent output
        if eq['is_unnumbered']:  # Unnumbered is also unreferenceable
            return None
        elif fmt in ['latex', 'beamer']:
            return RawInline('tex',
                             r'\begin{equation}%s\end{equation}'%value[-1])
        elif fmt in ('html', 'html5') and LABEL_PATTERN.match(label):
            # Present equation and its number in a span
            text = str(references[label])
            outerspan = RawInline('html',
                                  ''%(''\
                                  if eq['is_unreferenceable'] \
                                  else 'id="%s"'%label))
            innerspan = RawInline('html',
                                  '' %
                                  ('0' if text.startswith('$') and
                                   text.endswith('$') else '50%',))
            num = Math({"t":"InlineMath"}, '(%s)' % text[1:-1]) \
              if text.startswith('$') and text.endswith('$') \
              else Str('(%s)' % text)
            endspans = RawInline('html', '')
            return [outerspan, AttrMath(*value), innerspan, num, endspans]
        elif fmt == 'docx':
            # As per http://officeopenxml.com/WPhyperlink.php
            bookmarkstart = \
              RawInline('openxml',
                        '<w:bookmarkStart w:id="0" w:name="%s"/><w:r><w:t>'
                        %label)
            bookmarkend = \
              RawInline('openxml',
                        '</w:t></w:r><w:bookmarkEnd w:id="0"/>')
            return [bookmarkstart, AttrMath(*value), bookmarkend]

    return None

Response: Processes the attributed equations.


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

    # pylint: disable=global-statement
    global capitalize
    global use_cleveref_default
    global plusname
    global starname
    global numbersections

    # Read in the metadata fields and do some checking

    for name in ['eqnos-cleveref', 'xnos-cleveref', 'cleveref']:
        # 'xnos-cleveref' enables cleveref in all 3 of fignos/eqnos/tablenos
        # 'cleveref' is deprecated
        if name in meta:
            use_cleveref_default = check_bool(get_meta(meta, name))
            break

    for name in ['eqnos-capitalize', 'eqnos-capitalise',
                 'xnos-capitalize', 'xnos-capitalise']:
        # 'eqnos-capitalise' is an alternative spelling
        # 'xnos-capitalise' enables capitalise in all 3 of fignos/eqnos/tablenos
        # 'xnos-capitalize' is an alternative spelling
        if name in meta:
            capitalize = check_bool(get_meta(meta, name))
            break

    if 'eqnos-plus-name' in meta:
        tmp = get_meta(meta, 'eqnos-plus-name')
        if isinstance(tmp, list):
            plusname = tmp
        else:
            plusname[0] = tmp
        assert len(plusname) == 2
        for name in plusname:
            assert isinstance(name, STRTYPES)

    if 'eqnos-star-name' in meta:
        tmp = get_meta(meta, 'eqnos-star-name')
        if isinstance(tmp, list):
            starname = tmp
        else:
            starname[0] = tmp
        assert len(starname) == 2
        for name in starname:
            assert isinstance(name, STRTYPES)

    if 'xnos-number-sections' in meta:
        numbersections = check_bool(get_meta(meta, 'xnos-number-sections'))

Response:
Saves metadata fields in global variables and returns a few
    computed fields.


Instruction:
How would you code a function in Python 3 to
filter the document AST.

Response:
def main():
    """Filters the document AST."""

    # pylint: disable=global-statement
    global PANDOCVERSION
    global AttrMath

    # Get the output format and document
    fmt = args.fmt
    doc = json.loads(STDIN.read())

    # Initialize pandocxnos
    # pylint: disable=too-many-function-args
    PANDOCVERSION = pandocxnos.init(args.pandocversion, doc)

    # Element primitives
    AttrMath = elt('Math', 3)

    # Chop up the doc
    meta = doc['meta'] if PANDOCVERSION >= '1.18' else doc[0]['unMeta']
    blocks = doc['blocks'] if PANDOCVERSION >= '1.18' else doc[1:]

    # Process the metadata variables
    process(meta)

    # First pass
    attach_attrs_math = attach_attrs_factory(Math, allow_space=True)
    detach_attrs_math = detach_attrs_factory(Math)
    insert_secnos = insert_secnos_factory(Math)
    delete_secnos = delete_secnos_factory(Math)
    altered = functools.reduce(lambda x, action: walk(x, action, fmt, meta),
                               [attach_attrs_math, insert_secnos,
                                process_equations, delete_secnos,
                                detach_attrs_math], blocks)

    # Second pass
    process_refs = process_refs_factory(references.keys())
    replace_refs = replace_refs_factory(references,
                                        use_cleveref_default, use_eqref,
                                        plusname if not capitalize else
                                        [name.title() for name in plusname],
                                        starname, 'equation')
    altered = functools.reduce(lambda x, action: walk(x, action, fmt, meta),
                               [repair_refs, process_refs, replace_refs],
                               altered)

    # Update the doc
    if PANDOCVERSION >= '1.18':
        doc['blocks'] = altered
    else:
        doc = doc[:1] + altered

    # Dump the results
    json.dump(doc, STDOUT)

    # Flush stdout
    STDOUT.flush()


Instruction:
Can you generate a brief explanation for the following Python 3 code
def check_debug():
    from django.conf import settings

    if not settings.configured:
        return False

    # I _think_ this check is all that's needed and the 3 "hasattr" checks
    # below can be removed, but it's not clear how to verify that
    from django.apps import apps
    if not apps.ready:
        return False

    # django.template.backends.django gets loaded lazily, so return false
    # until they've been loaded
    if not hasattr(django.template, "backends"):
        return False
    if not hasattr(django.template.backends, "django"):
        return False
    if not hasattr(django.template.backends.django, "DjangoTemplates"):
        raise DjangoTemplatePluginException("Can't use non-Django templates.")

    for engine in django.template.engines.all():
        if not isinstance(engine, django.template.backends.django.DjangoTemplates):
            raise DjangoTemplatePluginException(
                "Can't use non-Django templates."
            )
        if not engine.engine.debug:
            raise DjangoTemplatePluginException(
                "Template debugging must be enabled in settings."
            )

    return True

Response: Checks that Django s template debugging is enabled and raises an exception if it is not.


Instruction:
Can you tell what is the following Python 3 function doing
def read_template_source(filename):
    # Import this late to be sure we don't trigger settings machinery too
    # early.
    from django.conf import settings

    if not settings.configured:
        settings.configure()

    with open(filename, "rb") as f:
        text = f.read().decode(settings.FILE_CHARSET)

    return text

Response: Read the source of a Django template returning the Unicode text.


Instruction:
Can you tell what is the following Python 3 function doing
def get_line_number(line_map, offset):
    for lineno, line_offset in enumerate(line_map, start=1):
        if line_offset > offset:
            return lineno
    return -1

Response: Find a line number given a line map and a character offset.


Instruction:
Write a Python 3 function for
dumping interesting information about this frame.

Response:
def dump_frame(frame, label=""):
    """Dump interesting information about this frame."""
    locals = dict(frame.f_locals)
    self = locals.get('self', None)
    context = locals.get('context', None)
    if "__builtins__" in locals:
        del locals["__builtins__"]

    if label:
        label = " ( %s ) " % label
    print("-- frame --%s---------------------" % label)
    print("{}:{}:{}".format(
        os.path.basename(frame.f_code.co_filename),
        frame.f_lineno,
        type(self),
        ))
    print(locals)
    if self:
        print("self:", self.__dict__)
    if context:
        print("context:", context.__dict__)
    print("\\--")


Instruction:
Can you generate a brief explanation for the following Python 3 code
def get_line_map(self, filename):
        if filename not in self.source_map:
            template_source = read_template_source(filename)
            if 0:   # change to see the template text
                for i in range(0, len(template_source), 10):
                    print("%3d: %r" % (i, template_source[i:i+10]))
            self.source_map[filename] = make_line_map(template_source)
        return self.source_map[filename]

Response: Returns a line map for filename.


Instruction:
Can you implement a function in Python 3 that
saves bytes to a file.

Response:
def bytes_to_file(input_data, output_file):
    """Save bytes to a file."""
    pathlib.Path(output_file.parent).mkdir(parents=True, exist_ok=True)

    with open(output_file, "wb") as file:
        file.write(input_data)


Instruction:
Can you generate a brief explanation for the following Python 3 code
def get_art(cache_dir, size, client):
    song = client.currentsong()

    if len(song) < 2:
        print("album: Nothing currently playing.")
        return

    file_name = f"{song['artist']}_{song['album']}_{size}.jpg".replace("/", "")
    file_name = cache_dir / file_name

    if file_name.is_file():
        shutil.copy(file_name, cache_dir / "current.jpg")
        print("album: Found cached art.")

    else:
        print("album: Downloading album art...")

        brainz.init()
        album_art = brainz.get_cover(song, size)

        if album_art:
            util.bytes_to_file(album_art, cache_dir / file_name)
            util.bytes_to_file(album_art, cache_dir / "current.jpg")

            print(f"album: Swapped art to {song['artist']}, {song['album']}.")

Response: Get the album art.


Instruction:
Write a Python 3 function that can
download the cover art.

Response:
def get_cover(song, size=250):
    """Download the cover art."""
    try:
        data = mus.search_releases(artist=song["artist"],
                                   release=song["album"],
                                   limit=1)
        release_id = data["release-list"][0]["release-group"]["id"]
        print(f"album: Using release-id: {data['release-list'][0]['id']}")

        return mus.get_release_group_image_front(release_id, size=size)

    except mus.NetworkError:
        get_cover(song, size)

    except mus.ResponseError:
        print("error: Couldn't find album art for",
              f"{song['artist']} - {song['album']}")


Instruction:
Explain what the following Python 3 code does
def get_args():
    description = "bum - Download and display album art \
                   for mpd tracks."
    arg = argparse.ArgumentParser(description=description)

    arg.add_argument("--size", metavar="\"px\"",
                     help="what size to display the album art in.",
                     default=250)

    arg.add_argument("--cache_dir", metavar="\"/path/to/dir\"",
                     help="Where to store the downloaded cover art.",
                     default=pathlib.Path.home() / ".cache/bum")

    arg.add_argument("--version", action="store_true",
                     help="Print \"bum\" version.")

    arg.add_argument("--port",
                     help="Use a custom mpd port.",
                     default=6600)

    arg.add_argument("--server",
                     help="Use a remote server instead of localhost.",
                     default="localhost")

    arg.add_argument("--no_display",
                     action="store_true",
                     help="Only download album art, don't display.")

    return arg.parse_args()

Response: Get the script arguments.


Instruction:
Can you create a Python 3 function that
converts from source format into target format. Relevant if converters are used in InputTemplates.

Response:
def convertforinput(self,filepath, metadata):
        """Convert from target format into one of the source formats. Relevant if converters are used in InputTemplates. Metadata already is metadata for the to-be-generated file. 'filepath' is both the source and the target file, the source file will be erased and overwritten with the conversion result!"""
        assert isinstance(metadata, CLAMMetaData) #metadata of the destination file (file to be generated here)
        if not metadata.__class__ in self.acceptforinput:
            raise Exception("Convertor " + self.__class__.__name__ + " can not convert input files to " + metadata.__class__.__name__ + "!")
        return False


Instruction:
Explain what the following Python 3 code does
def convertforoutput(self,outputfile):
        assert isinstance(outputfile, CLAMOutputFile) #metadata of the destination file (file to be generated here)
        if not outputfile.metadata.__class__ in self.acceptforoutput:
            raise Exception("Convertor " + self.__class__.__name__ + " can not convert input files to " + outputfile.metadata.__class__.__name__ + "!")
        return []

Response: Convert from one of the source formats into one of the target formats. Relevant if converters are used in OutputTemplates. Sourcefile is a CLAMOutputFile instance.


Instruction:
Make a summary of the following Python 3 code
def convertforinput(self,filepath, metadata=None):
        super(CharEncodingConverter,self).convertforinput(filepath, metadata)

        shutil.copy(filepath, filepath + '.convertsource')

        try:
            fsource = io.open(filepath + '.convertsource','r',encoding=self.charset)
            ftarget = io.open(filepath,'w',encoding=metadata['encoding'])
            for line in fsource:
                ftarget.write(line + "\n")
            success = True
        except:
            ftarget.close()
            fsource.fclose()
            success = False
        finally:
            os.unlink(filepath + '.convertsource')

        return success

Response: Convert from source format into one of the source formats. Relevant if converters are used in InputTemplates. Metadata already is metadata for the to - be - generated file.


Instruction:
How would you explain what the following Python 3 function does
def convertforoutput(self,outputfile):
        super(CharEncodingConverter,self).convertforoutput(outputfile)

        return withheaders( flask.make_response( ( line.encode(self.charset) for line in outputfile ) ) , 'text/plain; charset=' + self.charset)

Response: Convert from one of the source formats into one of the target formats. Relevant in OutputTemplates. Outputfile is a CLAMOutputFile instance.


Instruction:
Can you generate the documentation for the following Python 3 function
def getclamdata(filename, custom_formats=None):
    global CUSTOM_FORMATS  #pylint: disable=global-statement
    """This function reads the CLAM Data from an XML file. Use this to read
    the clam.xml file from your system wrapper. It returns a CLAMData instance.

    If you make use of CUSTOM_FORMATS, you need to pass the CUSTOM_FORMATS list as 2nd argument.
    """
    f = io.open(filename,'r',encoding='utf-8')
    xml = f.read(os.path.getsize(filename))
    f.close()
    if custom_formats:
        CUSTOM_FORMATS = custom_formats #dependency injection for CUSTOM_FORMATS
    return CLAMData(xml, None, True)

Response: This function reads the CLAM Data from an XML file.


Instruction:
How would you code a function in Python 3 to
sanitize the parameters for internal use only

Response:
def sanitizeparameters(parameters):
    """Construct a dictionary of parameters, for internal use only"""
    if not isinstance(parameters,dict):
        d = {}
        for x in parameters:
            if isinstance(x,tuple) and len(x) == 2:
                for parameter in x[1]:
                    d[parameter.id] = parameter
            elif isinstance(x, clam.common.parameters.AbstractParameter):
                d[x.id] = x
        return d
    else:
        return parameters


Instruction:
Write a Python 3 script for
giving input files and parameters produce metadata for outputfiles. Returns a list of matched profiles and a program.

Response:
def profiler(profiles, projectpath,parameters,serviceid,servicename,serviceurl,printdebug=None):
    """Given input files and parameters, produce metadata for outputfiles. Returns a list of matched profiles (empty if none match), and a program."""

    parameters = sanitizeparameters(parameters)

    matched = []
    program = Program(projectpath)
    for profile in profiles:
        if profile.match(projectpath, parameters)[0]:
            matched.append(profile)
            program.update( profile.generate(projectpath,parameters,serviceid,servicename,serviceurl) )

    return matched, program


Instruction:
Implement a Python 3 function for
returning the value string wrapped in the specified quotes if not empty

Response:
def shellsafe(s, quote='', doescape=True):
    """Returns the value string, wrapped in the specified quotes (if not empty), but checks and raises an Exception if the string is at risk of causing code injection"""
    if sys.version[0] == '2' and not isinstance(s,unicode): #pylint: disable=undefined-variable
        s = unicode(s,'utf-8') #pylint: disable=undefined-variable
    if len(s) > 1024:
        raise ValueError("Variable value rejected for security reasons: too long")
    if quote:
        if quote in s:
            if doescape:
                s = escape(s,quote)
            else:
                raise ValueError("Variable value rejected for security reasons: " + s)
        return quote + s + quote
    else:
        for c in s:
            if c in DISALLOWINSHELLSAFE:
                raise ValueError("Variable value rejected for security reasons: " + s)
        return s


Instruction:
Write a Python 3 script to
attach viewers and converters to file automatically scan all profiles for outputtemplate or inputtemplate

Response:
def attachviewers(self, profiles):
        """Attach viewers *and converters* to file, automatically scan all profiles for outputtemplate or inputtemplate"""
        if self.metadata:
            template = None
            for profile in profiles:
                if isinstance(self, CLAMInputFile):
                    for t in profile.input:
                        if self.metadata.inputtemplate == t.id:
                            template = t
                            break
                elif isinstance(self, CLAMOutputFile) and self.metadata and self.metadata.provenance:
                    for t in profile.outputtemplates():
                        if self.metadata.provenance.outputtemplate_id == t.id:
                            template = t
                            break
                else:
                    raise NotImplementedError #Is ok, nothing to implement for now
                if template:
                    break
            if template and template.viewers:
                for viewer in template.viewers:
                    self.viewers.append(viewer)
            if template and template.converters:
                for converter in template.converters:
                    self.converters.append(converter)


Instruction:
Can you implement a function in Python 3 that
returns the filename for the metadata file. Only used for local files. Only used for local files.

Response:
def metafilename(self):
        """Returns the filename for the metadata file (not full path). Only used for local files."""
        metafilename = os.path.dirname(self.filename)
        if metafilename: metafilename += '/'
        metafilename += '.' + os.path.basename(self.filename) + '.METADATA'
        return metafilename


Instruction:
Explain what the following Python 3 code does
def loadmetadata(self):
        if not self.remote:
            metafile = self.projectpath + self.basedir + '/' + self.metafilename()
            if os.path.exists(metafile):
                f = io.open(metafile, 'r',encoding='utf-8')
                xml = "".join(f.readlines())
                f.close()
            else:
                raise IOError(2, "No metadata found, expected " + metafile )
        else:
            if self.client:
                requestparams = self.client.initrequest()
            else:
                requestparams = {}
            response = requests.get(self.projectpath + self.basedir + '/' + self.filename + '/metadata', **requestparams)
            if response.status_code != 200:
                extramsg = ""
                if not self.client: extramsg = "No client was associated with this CLAMFile, associating a client is necessary when authentication is needed"
                raise HTTPError(2, "Can't download metadata for " + self.filename + ". " + extramsg)
            xml = response.text

        #parse metadata
        try:
            self.metadata = CLAMMetaData.fromxml(xml, self) #returns CLAMMetaData object (or child thereof)
        except ElementTree.XMLSyntaxError:
            raise ValueError("Metadata is not XML! Contents: " + xml)

Response: Load metadata for this file. This is usually called automatically upon instantiation except if explicitly disabled. Works both locally as well as for clients connecting to a CLAM service.


Instruction:
Implement a Python 3 function for
loading all lines in memory

Response:
def read(self):
        """Loads all lines in memory"""
        lines = self.readlines()
        if self.metadata and 'encoding' in self.metadata:
            encoding = self.metadata['encoding']
        else:
            encoding = 'utf-8'
        if sys.version < '3':
            return "\n".join( unicode(line, 'utf-8') if isinstance(line, str) else line for line in lines)
        else:
            return "\n".join( str(line, 'utf-8') if isinstance(line, bytes) else line for line in lines)


Instruction:
Explain what the following Python 3 code does
def copy(self, target, timeout=500):

        if self.metadata and 'encoding' in self.metadata:
            with io.open(target,'w', encoding=self.metadata['encoding']) as f:
                for line in self:
                    f.write(line)
        else:
            with io.open(target,'wb') as f:
                for line in self:
                    if sys.version < '3' and isinstance(line,unicode): #pylint: disable=undefined-variable
                        f.write(line.encode('utf-8'))
                    elif sys.version >= '3' and isinstance(line,str):
                        f.write(line.encode('utf-8'))
                    else:
                        f.write(line)

Response: Copy or download this file to a new local file


Instruction:
Create a Python 3 function to
parse CLAM XML and sets attributes of this object.

Response:
def parseresponse(self, xml, localroot = False):
        """Parses CLAM XML, there's usually no need to call this directly"""
        root = parsexmlstring(xml)
        if root.tag != 'clam':
            raise FormatError("CLAM root tag not found")

        self.system_id = root.attrib['id']
        self.system_name = root.attrib['name']
        if 'project' in root.attrib:
            self.project = root.attrib['project']
        else:
            self.project = None
        if 'baseurl' in root.attrib:
            self.baseurl = root.attrib['baseurl']
            if self.project:
                if localroot is True: #implicit, assume CWD
                    self.remote = False
                    self.projecturl = '' #relative directory (assume CWD is project directory, as is the case when wrapper scripts are called)
                elif localroot: #explicit
                    self.remote = False
                    self.projecturl = localroot + '/' + self.project + '/' #explicit directory
                else:
                    self.remote = True #no directory: remote URL
                    self.projecturl = self.baseurl + '/' + self.project + '/'

        if 'user' in root.attrib:
            self.user = root.attrib['user']
        else:
            self.user = None

        self.oauth_access_token = ""
        if 'oauth_access_token' in root.attrib:
            self.oauth_access_token = root.attrib['oauth_access_token']

        for node in root: #pylint: disable=too-many-nested-blocks
            if node.tag == "description":
                self.description = node.text
            elif node.tag == "version":
                self.system_version = node.text
            elif node.tag == "email":
                self.system_email = node.text
            elif node.tag == 'status':
                self.status = int(node.attrib['code'])
                self.statusmessage  = node.attrib['message']
                self.completion  = node.attrib['completion']
                if 'errors' in node.attrib:
                    self.errors = ((node.attrib['errors'] == 'yes') or (node.attrib['errors'] == '1'))
                if 'errormsg' in node.attrib:
                    self.errormsg = node.attrib['errormsg']
            elif node.tag == 'parameters':
                for parametergroupnode in node:
                    if parametergroupnode.tag == 'parametergroup':
                        parameterlist = []
                        for parameternode in parametergroupnode:
                            if parameternode.tag in vars(clam.common.parameters):
                                parameterlist.append(vars(clam.common.parameters)[parameternode.tag].fromxml(parameternode))
                            else:
                                raise Exception("Expected parameter class '" + parameternode.tag + "', but not defined!")
                        self.parameters.append( (parametergroupnode.attrib['name'], parameterlist) )
            elif node.tag == 'corpora':
                for corpusnode in node:
                    if corpusnode.tag == 'corpus':
                        self.corpora.append(corpusnode.value)
            elif node.tag == 'profiles':
                for subnode in node:
                    if subnode.tag == 'profile':
                        self.profiles.append(Profile.fromxml(subnode))
            elif node.tag == 'input':
                for filenode in node:
                    if filenode.tag == 'file':
                        for n in filenode:
                            if n.tag == 'name':
                                self.input.append( CLAMInputFile( self.projecturl, n.text, self.loadmetadata, self.client,True) )
            elif node.tag == 'output':
                for filenode in node:
                    if filenode.tag == 'file':
                        for n in filenode:
                            if n.tag == 'name':
                                self.output.append( CLAMOutputFile( self.projecturl, n.text, self.loadmetadata, self.client ) )
            elif node.tag == 'projects':
                self.projects = []
                for projectnode in node:
                    if projectnode.tag == 'project':
                        self.projects.append(projectnode.text)
            elif node.tag == 'program':
                self.program = Program(self.projecturl, [ int(i) for i in node.attrib['matchedprofiles'].split(',') ] )
                for outputfilenode in node:
                    if outputfilenode.tag == 'outputfile':
                        inputfound = False
                        for inputfilenode in outputfilenode:
                            if inputfilenode.tag == 'inputfile':
                                inputfound = True
                                self.program.add(outputfilenode.attrib['name'],outputfilenode.attrib['template'],inputfilenode.attrib['name'], inputfilenode.attrib['template'])
                        if not inputfound:
                            self.program.add(outputfilenode.attrib['name'],outputfilenode.attrib['template'])


Instruction:
Create a Python 3 function for
getting an output template by ID

Response:
def outputtemplate(self, template_id):
        """Get an output template by ID"""
        for profile in self.profiles:
            for outputtemplate in profile.outputtemplates():
                if outputtemplate.id == template_id:
                    return outputtemplate
        return KeyError("Outputtemplate " + template_id + " not found")


Instruction:
Create a Python 3 function for
obtaining a string of all parameters using the paramater flags they were defined with in order to pass to an external command. This is shell - safe by definition.

Response:
def commandlineargs(self):
        """Obtain a string of all parameters, using the paramater flags they were defined with, in order to pass to an external command. This is shell-safe by definition."""
        commandlineargs = []
        for parametergroup, parameters in self.parameters: #pylint: disable=unused-variable
            for parameter in parameters:
                p = parameter.compilearg()
                if p:
                    commandlineargs.append(p)
        return " ".join(commandlineargs)


Instruction:
Implement a Python 3 function for
returning the specified global parameter

Response:
def parameter(self, parameter_id):
        """Return the specified global parameter (the entire object, not just the value)"""
        for parametergroup, parameters in self.parameters: #pylint: disable=unused-variable
            for parameter in parameters:
                if parameter.id == parameter_id:
                    return parameter
        raise KeyError("No such parameter exists: " + parameter_id )


Instruction:
Given the following Python 3 function, write the documentation
def parametererror(self):
        for parametergroup, parameters in self.parameters: #pylint: disable=unused-variable
            for parameter in parameters:
                if parameter.error:
                    return parameter.error
        return False

Response: Return the first parameter error


Instruction:
Can you implement a function in Python 3 that
returns all parameters as a dictionary

Response:
def passparameters(self):
        """Return all parameters as {id: value} dictionary"""
        paramdict = {}
        for parametergroup, params in self.parameters: #pylint: disable=unused-variable
            for parameter in params:
                if parameter.value:
                    if isinstance(parameter.value, list):
                        paramdict[parameter.id] = ",".join(parameter.value)
                    else:
                        paramdict[parameter.id] = parameter.value
        return paramdict


Instruction:
How would you explain what the following Python 3 function does
def inputtemplates(self):
        l = []
        for profile in self.profiles:
            l += profile.input
        return l

Response: Return all input templates as a list of InputTemplate instances


Instruction:
Write a Python 3 script to
return the inputtemplate with the specified ID. This is used to resolve a inputtemplate ID to an InputTemplate object instance instance.

Response:
def inputtemplate(self,template_id):
        """Return the inputtemplate with the specified ID. This is used to resolve a inputtemplate ID to an InputTemplate object instance"""
        for profile in self.profiles:
            for inputtemplate in profile.input:
                if inputtemplate.id == template_id:
                    return inputtemplate
        raise Exception("No such input template: " + repr(template_id))


Instruction:
Explain what the following Python 3 code does
def inputfile(self, inputtemplate=None):
        inputfiles = list(self.inputfiles(inputtemplate))
        if len(inputfiles) < 1:
            raise Exception("No such input file")
        elif len(inputfiles) > 1:
            raise Exception("Multiple input files matched. Use inputfiles() instead!")
        return inputfiles[0]

Response: Return the inputfile for the specified inputtemplate if inputtemplate = None inputfile is returned regardless of inputtemplate.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def inputfiles(self, inputtemplate=None):
        if isinstance(inputtemplate, InputTemplate):
            #ID suffices:
            inputtemplate = inputtemplate.id
        for inputfile in self.input:
            if not inputtemplate or inputfile.metadata.inputtemplate == inputtemplate:
                yield inputfile

Response: Generator yielding all inputfiles for the specified inputtemplate.


Instruction:
Can you tell what is the following Python 3 function doing
def match(self, projectpath, parameters):
        parameters = sanitizeparameters(parameters)

        mandatory_absent = [] #list of input templates that are missing but mandatory
        optional_absent = [] #list of absent but optional input templates

        #check if profile matches inputdata (if there are no inputtemplate, this always matches intentionally!)
        for inputtemplate in self.input:
            if not inputtemplate.matchingfiles(projectpath):
                if inputtemplate.optional:
                    optional_absent.append(inputtemplate)
                else:
                    mandatory_absent.append(inputtemplate)

        if mandatory_absent:
            return False, mandatory_absent

        #check if output is produced
        if not self.output: return False, mandatory_absent
        for outputtemplate in self.output:
            if isinstance(outputtemplate, ParameterCondition):
                outputtemplate = outputtemplate.evaluate(parameters)
            if not outputtemplate:
                continue
            assert isinstance(outputtemplate, OutputTemplate)
            if outputtemplate.parent:
                if outputtemplate.getparent(self) not in optional_absent: #pylint: disable=protected-access
                    return True, optional_absent
            else:
                return True, optional_absent

        return False, optional_absent

Response: Check if the profile matches all inputdata and produces output given the set parameters. Returns a boolean


Instruction:
Can you implement a function in Python 3 that
returns a list of all inputfiles matching the profile

Response:
def matchingfiles(self, projectpath):
        """Return a list of all inputfiles matching the profile (filenames)"""
        l = []
        for inputtemplate in self.input:
            l += inputtemplate.matchingfiles(projectpath)
        return l


Instruction:
Can you create a Python 3 function that
returns all outputtemplates resolving ParameterConditions to all possibilities

Response:
def outputtemplates(self):
        """Returns all outputtemplates, resolving ParameterConditions to all possibilities"""
        outputtemplates = []
        for o in self.output:
            if isinstance(o, ParameterCondition):
                outputtemplates += o.allpossibilities()
            else:
                assert isinstance(o, OutputTemplate)
                outputtemplates.append(o)
        return outputtemplates


Instruction:
Explain what the following Python 3 code does
def generate(self, projectpath, parameters, serviceid, servicename,serviceurl):

        #Make dictionary of parameters
        parameters = sanitizeparameters(parameters)

        program = Program(projectpath, [self])

        match, optional_absent = self.match(projectpath, parameters) #Does the profile match?
        if match: #pylint: disable=too-many-nested-blocks

            #gather all input files that match
            inputfiles = self.matchingfiles(projectpath) #list of (seqnr, filename,inputtemplate) tuples

            inputfiles_full = [] #We need the full CLAMInputFiles for generating provenance data
            for seqnr, filename, inputtemplate in inputfiles: #pylint: disable=unused-variable
                inputfiles_full.append(CLAMInputFile(projectpath, filename))

            for outputtemplate in self.output:
                if isinstance(outputtemplate, ParameterCondition):
                    outputtemplate = outputtemplate.evaluate(parameters)

                #generate output files
                if outputtemplate:
                    if isinstance(outputtemplate, OutputTemplate):
                        #generate provenance data
                        provenancedata = CLAMProvenanceData(serviceid,servicename,serviceurl,outputtemplate.id, outputtemplate.label,  inputfiles_full, parameters)

                        create = True
                        if outputtemplate.parent:
                            if outputtemplate.getparent(self) in optional_absent:
                                create = False

                        if create:
                            for inputtemplate, inputfilename, outputfilename, metadata in outputtemplate.generate(self, parameters, projectpath, inputfiles, provenancedata):
                                clam.common.util.printdebug("Writing metadata for outputfile " + outputfilename)
                                metafilename = os.path.dirname(outputfilename)
                                if metafilename: metafilename += '/'
                                metafilename += '.' + os.path.basename(outputfilename) + '.METADATA'
                                f = io.open(projectpath + '/output/' + metafilename,'w',encoding='utf-8')
                                f.write(metadata.xml())
                                f.close()
                                program.add(outputfilename, outputtemplate, inputfilename, inputtemplate)
                    else:
                        raise TypeError("OutputTemplate expected, but got " + outputtemplate.__class__.__name__)

        return program

Response: Generate output metadata on the basis of input files and parameters. Projectpath must be absolute. Returns a Program instance.


Instruction:
Implement a function in Python 3 to
produce XML output for the profile

Response:
def xml(self, indent = ""):
        """Produce XML output for the profile"""
        xml = "\n" + indent + "<profile>\n"
        xml += indent + " <input>\n"
        for inputtemplate in self.input:
            xml += inputtemplate.xml(indent +"    ") + "\n"
        xml += indent + " </input>\n"
        xml += indent + " <output>\n"
        for outputtemplate in self.output:
            xml += outputtemplate.xml(indent +"    ") + "\n" #works for ParameterCondition as well!
        xml += indent + " </output>\n"
        xml += indent + "</profile>\n"
        return xml


Instruction:
Create a Python 3 function to
return a profile instance from the given XML description. Node can be a string or an etree. _Element.

Response:
def fromxml(node):
        """Return a profile instance from the given XML description. Node can be a string or an etree._Element."""
        if not isinstance(node,ElementTree._Element): #pylint: disable=protected-access
            node = parsexmlstring(node)

        args = []

        if node.tag == 'profile':
            for node in node:
                if node.tag == 'input':
                    for subnode in node:
                        if subnode.tag.lower() == 'inputtemplate':
                            args.append(InputTemplate.fromxml(subnode))
                elif node.tag == 'output':
                    for subnode in node:
                        if subnode.tag.lower() == 'outputtemplate':
                            args.append(OutputTemplate.fromxml(subnode))
                        elif subnode.tag.lower() == 'parametercondition':
                            args.append(ParameterCondition.fromxml(subnode))
        return Profile(*args)


Instruction:
Write a Python 3 function that can
add a new path to the program

Response:
def add(self, outputfilename, outputtemplate, inputfilename=None, inputtemplate=None):
        """Add a new path to the program"""
        if isinstance(outputtemplate,OutputTemplate):
            outputtemplate = outputtemplate.id
        if isinstance(inputtemplate,InputTemplate):
            inputtemplate = inputtemplate.id
        if outputfilename in self:
            outputtemplate, inputfiles = self[outputfilename]
            if inputfilename and inputtemplate:
                inputfiles[inputfilename] = inputtemplate
        else:
            if inputfilename and inputtemplate:
                self[outputfilename] = (outputtemplate, {inputfilename: inputtemplate})
            else:
                self[outputfilename] = (outputtemplate, {})


Instruction:
Here you have a function in Python 3, explain what it does
def getoutputfiles(self, loadmetadata=True, client=None,requiremetadata=False):
        for outputfilename, outputtemplate in self.outputpairs():
            yield CLAMOutputFile(self.projectpath, outputfilename, loadmetadata,client,requiremetadata), outputtemplate

Response: Iterates over all output files and their output templates.


Instruction:
How would you code a function in Python 3 to
iterate over all input files for the specified outputfile.

Response:
def getinputfiles(self, outputfile, loadmetadata=True, client=None,requiremetadata=False):
        """Iterates over all input files for the specified outputfile (you may pass a CLAMOutputFile instance or a filename string). Yields (CLAMInputFile,str:inputtemplate_id) tuples. The last three arguments are passed to its constructor."""
        if isinstance(outputfile, CLAMOutputFile):
            outputfilename = str(outputfile).replace(os.path.join(self.projectpath,'output/'),'')
        else:
            outputfilename = outputfile
        outputtemplate, inputfiles = self[outputfilename]
        for inputfilename, inputtemplate in inputfiles.items():
            yield CLAMInputFile(self.projectpath, inputfilename, loadmetadata,client,requiremetadata), inputtemplate


Instruction:
Can you create a Python 3 function that
gets one input file for the specified output filename.

Response:
def getinputfile(self, outputfile, loadmetadata=True, client=None,requiremetadata=False):
        """Grabs one input file for the specified output filename (raises a KeyError exception if there is no such output, StopIteration if there are no input files for it). Shortcut for getinputfiles()"""
        if isinstance(outputfile, CLAMOutputFile):
            outputfilename = str(outputfile).replace(os.path.join(self.projectpath,'output/'),'')
        else:
            outputfilename = outputfile
        if outputfilename not in self:
            raise KeyError("No such outputfile " + outputfilename)
        try:
            return next(self.getinputfiles(outputfile,loadmetadata,client,requiremetadata))
        except StopIteration:
            raise StopIteration("No input files for outputfile " + outputfilename)


Instruction:
Can you tell what is the following Python 3 function doing
def getoutputfile(self, loadmetadata=True, client=None,requiremetadata=False):
        return next(self.getoutputfiles(loadmetadata,client,requiremetadata))

Response: Grabs one output file.


Instruction:
Write a Python 3 function that can
serialise provenance data to XML. This is included in CLAM Metadata files.

Response:
def xml(self, indent = ""):
        """Serialise provenance data to XML. This is included in CLAM Metadata files"""
        xml = indent + "<provenance type=\"clam\" id=\""+self.serviceid+"\" name=\"" +self.servicename+"\" url=\"" + self.serviceurl+"\" outputtemplate=\""+self.outputtemplate_id+"\" outputtemplatelabel=\""+self.outputtemplate_label+"\" timestamp=\""+str(self.timestamp)+"\">"
        for filename, metadata in self.inputfiles:
            xml += indent + " <inputfile name=\"" + clam.common.util.xmlescape(filename) + "\">"
            xml += metadata.xml(indent + " ") + "\n"
            xml += indent +  " </inputfile>\n"
        if self.parameters:
            xml += indent + " <parameters>\n"
            if isinstance(self.parameters, dict):
                parameters = self.parameters.values()
            elif isinstance(self.parameters, list):
                parameters = self.parameters
            for parameter in parameters:
                xml += parameter.xml(indent +"  ") + "\n"
            xml += indent + " </parameters>\n"
        xml += indent + "</provenance>"
        return xml


Instruction:
Can you generate a brief explanation for the following Python 3 code
def fromxml(node):
        if not isinstance(node,ElementTree._Element): #pylint: disable=protected-access
            node = parsexmlstring(node)
        if node.tag == 'provenance': #pylint: disable=too-many-nested-blocks
            if node.attrib['type'] == 'clam':
                serviceid = node.attrib['id']
                servicename = node.attrib['name']
                serviceurl = node.attrib['url']
                timestamp = node.attrib['timestamp']
                outputtemplate = node.attrib['outputtemplate']
                outputtemplatelabel = node.attrib['outputtemplatelabel']
                inputfiles = []
                parameters = []
                for subnode in node:
                    if subnode.tag == 'inputfile':
                        filename = node.attrib['name']
                        metadata = None
                        for subsubnode in subnode:
                            if subsubnode.tag == 'CLAMMetaData':
                                metadata = CLAMMetaData.fromxml(subsubnode)
                                break
                        inputfiles.append( (filename, metadata) )
                    elif subnode.tag == 'parameters':
                        for subsubnode in subnode:
                            if subsubnode.tag in vars(clam.common.parameters):
                                parameters.append(vars(clam.common.parameters)[subsubnode.tag].fromxml(subsubnode))
                            else:
                                raise Exception("Expected parameter class '" + subsubnode.tag + "', but not defined!")
                return CLAMProvenanceData(serviceid,servicename,serviceurl,outputtemplate, outputtemplatelabel, inputfiles, parameters, timestamp)
            else:
                raise NotImplementedError

Response: Return a CLAMProvenanceData instance from the given XML description. Node can be a string or an lxml. etree. _Element.


Instruction:
How would you implement a function in Python 3 that
renders an XML representation of the metadata

Response:
def xml(self, indent = ""):
        """Render an XML representation of the metadata""" #(independent of web.py for support in CLAM API)
        if not indent:
            xml = '<?xml version="1.0" encoding="UTF-8"?>\n'
        else:
            xml = ""
        xml += indent + "<CLAMMetaData format=\"" + self.__class__.__name__ + "\""
        if self.mimetype:
            xml += " mimetype=\""+self.mimetype+"\""
        if self.schema:
            xml += " schema=\""+self.schema+"\""
        if self.inputtemplate:
            xml += " inputtemplate=\""+self.inputtemplate+"\""
        xml += ">\n"

        for key, value in self.data.items():
            xml += indent + "  <meta id=\""+clam.common.util.xmlescape(key)+"\">"+clam.common.util.xmlescape(str(value))+"</meta>\n"

        if self.provenance:
            xml += self.provenance.xml(indent + "  ")

        xml += indent +  "</CLAMMetaData>"
        return xml


Instruction:
Here you have a function in Python 3, explain what it does
def save(self, filename):
        with io.open(filename,'w',encoding='utf-8') as f:
            f.write(self.xml())

Response: Save the current metadata to a file


Instruction:
Implement a Python 3 function for
reading metadata from XML. Static method returning an CLAMMetaData instance (or rather; the appropriate subclass of CLAMMetaData) from the given XML description. Node can be a string or an etree._Element.

Response:
def fromxml(node, file=None):
        """Read metadata from XML. Static method returning an CLAMMetaData instance (or rather; the appropriate subclass of CLAMMetaData) from the given XML description. Node can be a string or an etree._Element."""
        if not isinstance(node,ElementTree._Element): #pylint: disable=protected-access
            node = parsexmlstring(node)
        if node.tag == 'CLAMMetaData':
            dataformat = node.attrib['format']

            formatclass = None
            for C in CUSTOM_FORMATS: #CUSTOM_FORMATS will be injected by clamservice.py
                if C.__name__ == dataformat:
                    formatclass = C
                    break
            if formatclass is None and dataformat in vars(clam.common.formats) and issubclass(vars(clam.common.formats)[dataformat], CLAMMetaData):
                formatclass = vars(clam.common.formats)[dataformat]
            if formatclass is None:
                raise Exception("Format class " + dataformat + " not found!")

            data = {}
            if 'inputtemplate' in node.attrib:
                data['inputtemplate'] = node.attrib['inputtemplate']
            if 'inputtemplatelabel' in node.attrib:
                data['inputtemplatelabel'] = node.attrib['inputtemplatelabel']

            for subnode in node:
                if subnode.tag == 'meta':
                    key = subnode.attrib['id']
                    value = subnode.text
                    data[key] = value
                elif subnode.tag == 'provenance':
                    data['provenance'] = CLAMProvenanceData.fromxml(subnode)
            return formatclass(file, **data)
        else:
            raise Exception("Invalid CLAM Metadata!")


Instruction:
How would you implement a function in Python 3 that
produces XML representation of the object.

Response:
def xml(self, indent = ""):
        """Produce Template XML"""
        xml = indent + "<InputTemplate id=\""+self.id+"\" format=\"" + self.formatclass.__name__ + "\"" + " label=\"" + self.label + "\""
        if self.formatclass.mimetype:
            xml +=" mimetype=\""+self.formatclass.mimetype+"\""
        if self.formatclass.schema:
            xml +=" schema=\""+self.formatclass.schema+"\""
        if self.filename:
            xml +=" filename=\""+self.filename+"\""
        if self.extension:
            xml +=" extension=\""+self.extension+"\""
        if self.optional:
            xml +=" optional=\"yes\""
        else:
            xml +=" optional=\"no\""
        if self.unique:
            xml +=" unique=\"yes\""
        else:
            xml +=" unique=\"no\""
        if self.acceptarchive:
            xml +=" acceptarchive=\"yes\""
        else:
            xml +=" acceptarchive=\"no\""
        xml += ">\n"
        for parameter in self.parameters:
            xml += parameter.xml(indent+"    ") + "\n"
        if self.converters:
            for converter in self.converters:
                xml += indent + "    <converter id=\""+converter.id+"\">"+clam.common.util.xmlescape(converter.label)+"</converter>\n"
        if self.inputsources:
            for inputsource in self.inputsources:
                xml += inputsource.xml(indent+"    ")
        xml += indent + "</InputTemplate>"
        return xml


Instruction:
Explain what the following Python 3 code does
def json(self):
        d = { 'id': self.id, 'format': self.formatclass.__name__,'label': self.label, 'mimetype': self.formatclass.mimetype,  'schema': self.formatclass.schema }
        if self.unique:
            d['unique'] = True
        if self.filename:
            d['filename'] = self.filename
        if self.extension:
            d['extension'] = self.extension
        if self.acceptarchive:
            d['acceptarchive'] = self.acceptarchive
        #d['parameters'] = {}

        #The actual parameters are included as XML, and transformed by clam.js using XSLT (parameter.xsl) to generate the forms
        parametersxml = ''
        for parameter in self.parameters:
            parametersxml += parameter.xml()
        d['parametersxml'] = '<?xml version="1.0" encoding="utf-8" ?><parameters>' + parametersxml + '</parameters>'
        d['converters'] = [ {'id':x.id, 'label':x.label} for x in self.converters ]
        d['inputsources'] = [ {'id':x.id, 'label':x.label} for x in self.inputsources ]

        return json.dumps(d)

Response: Produce a JSON representation of the web interface


Instruction:
How would you explain what the following Python 3 function does
def match(self, metadata, user = None):
        assert isinstance(metadata, self.formatclass)
        return self.generate(metadata,user)

Response: Does the specified metadata match this template? returns success metadata and parameters


Instruction:
Make a summary of the following Python 3 code
def matchingfiles(self, projectpath):
        results = []

        if projectpath[-1] == '/':
            inputpath = projectpath + 'input/'
        else:
            inputpath = projectpath + '/input/'

        for linkf,realf in clam.common.util.globsymlinks(inputpath + '/.*.INPUTTEMPLATE.' + self.id + '.*'):
            seqnr = int(linkf.split('.')[-1])
            results.append( (seqnr, realf[len(inputpath):], self) )
        results = sorted(results)
        if self.unique and len(results) != 1:
            return []
        else:
            return results

Response: Checks if the input conditions are satisfied. Returns a list of matching results.


Instruction:
Implement a function in Python 3 to
validate the posted data against the inputtemplate

Response:
def validate(self, postdata, user = None):
        """Validate posted data  against the inputtemplate"""
        clam.common.util.printdebug("Validating inputtemplate " + self.id + "...")
        errors, parameters, _  = processparameters(postdata, self.parameters, user)
        return errors, parameters


Instruction:
Create a Python 3 function to
convert the template into instantiated metadata validating the data in the process and returning errors otherwise.

Response:
def generate(self, file, validatedata = None,  inputdata=None, user = None):
        """Convert the template into instantiated metadata, validating the data in the process and returning errors otherwise. inputdata is a dictionary-compatible structure, such as the relevant postdata. Return (success, metadata, parameters), error messages can be extracted from parameters[].error. Validatedata is a (errors,parameters) tuple that can be passed if you did validation in a prior stage, if not specified, it will be done automatically."""

        metadata = {}

        if not validatedata:
            assert inputdata
            errors, parameters = self.validate(inputdata,user) #pylint: disable=unused-variable
        else:
            errors, parameters = validatedata #pylint: disable=unused-variable

        #scan errors and set metadata
        success = True
        for parameter in parameters:
            assert isinstance(parameter, clam.common.parameters.AbstractParameter)
            if parameter.error:
                success = False
            else:
                metadata[parameter.id] = parameter.value

        if not success:
            metadata = None
        else:
            try:
                metadata = self.formatclass(file, **metadata)
            except:
                raise

        return success, metadata, parameters


Instruction:
Can you generate a brief explanation for the following Python 3 code
def xml(self, operator='set', indent = ""):
        xml = indent + "<meta id=\"" + self.key + "\""
        if operator != 'set':
            xml += " operator=\"" + operator + "\""
        if not self.value:
            xml += " />"
        else:
            xml += ">" + self.value + "</meta>"
        return xml

Response: Serialize the metadata field to XML


Instruction:
Can you generate the documentation for the following Python 3 function
def fromxml(node):
        if not isinstance(node,ElementTree._Element): #pylint: disable=protected-access
            node = parsexmlstring(node)
        if node.tag.lower() != 'meta':
            raise Exception("Expected meta tag but got '" + node.tag + "' instead")

        key = node.attrib['id']
        if node.text:
            value = node.text
        else:
            value = None
        operator = 'set'
        if 'operator' in node.attrib:
            operator= node.attrib['operator']
        if operator == 'set':
            cls = SetMetaField
        elif operator == 'unset':
            cls = UnsetMetaField
        elif operator == 'copy':
            cls = CopyMetaField
        elif operator == 'parameter':
            cls = ParameterMetaField
        return cls(key, value)

Response: Static method returning an instance of MetaField from the given XML description. Node can be a string or an etree. _Element object.


Instruction:
Implement a Python 3 function for
finding the most suitable parent for this inputtemplate

Response:
def findparent(self, inputtemplates):
        """Find the most suitable parent, that is: the first matching unique/multi inputtemplate"""
        for inputtemplate in inputtemplates:
            if self.unique == inputtemplate.unique:
                return inputtemplate.id
        return None


Instruction:
Can you write a function in Python 3 where it
resolves a parent ID

Response:
def getparent(self, profile):
        """Resolve a parent ID"""
        assert self.parent
        for inputtemplate in profile.input:
            if inputtemplate == self.parent:
                return inputtemplate
        raise Exception("Parent InputTemplate '"+self.parent+"' not found!")


Instruction:
Given the following Python 3 function, write the documentation
def generate(self, profile, parameters, projectpath, inputfiles, provenancedata=None):

        project = os.path.basename(projectpath)

        if self.parent: #pylint: disable=too-many-nested-blocks
            #We have a parent, infer the correct filename

            #copy filename from parent
            parent = self.getparent(profile)

            #get input files for the parent InputTemplate
            parentinputfiles = parent.matchingfiles(projectpath)
            if not parentinputfiles:
                raise Exception("OutputTemplate '"+self.id + "' has parent '" + self.parent + "', but no matching input files were found!")

            #Do we specify a full filename?
            for seqnr, inputfilename, inputtemplate in parentinputfiles: #pylint: disable=unused-variable

                if self.filename:
                    filename = self.filename
                    parentfile = CLAMInputFile(projectpath, inputfilename)
                elif parent:
                    filename = inputfilename
                    parentfile = CLAMInputFile(projectpath, inputfilename)
                else:
                    raise Exception("OutputTemplate '"+self.id + "' has no parent nor filename defined!")

                #Make actual CLAMInputFile objects of ALL relevant input files, that is: all unique=True files and all unique=False files with the same sequence number
                relevantinputfiles = []
                for seqnr2, inputfilename2, inputtemplate2 in inputfiles:
                    if seqnr2 == 0 or seqnr2 == seqnr:
                        relevantinputfiles.append( (inputtemplate2, CLAMInputFile(projectpath, inputfilename2)) )

                #resolve # in filename (done later)
                #if not self.unique:
                #    filename.replace('#',str(seqnr))

                if not self.filename and self.removeextensions:
                    #Remove unwanted extensions
                    if self.removeextensions is True:
                        #Remove any and all extensions
                        filename = filename.split('.')[0]
                    elif isinstance(self.removeextensions, list):
                        #Remove specified extension
                        for ext in self.removeextensions:
                            if ext:
                                if ext[0] != '.' and filename[-len(ext) - 1:] == '.' + ext:
                                    filename = filename[:-len(ext) - 1]
                                elif ext[0] == '.' and filename[-len(ext):] == ext:
                                    filename = filename[:-len(ext)]

                if self.extension and not self.filename and filename[-len(self.extension) - 1:] != '.' + self.extension: #(also prevents duplicate extensions)
                    filename += '.' + self.extension

                #Now we create the actual metadata
                metadata = self.generatemetadata(parameters, parentfile, relevantinputfiles, provenancedata)

                #Resolve filename
                filename = resolveoutputfilename(filename, parameters, metadata, self, seqnr, project, inputfilename)

                yield inputtemplate, inputfilename, filename, metadata

        elif self.unique and self.filename:
            #outputtemplate has no parent, but specified a filename and is unique, this implies it is not dependent on input files:

            metadata = self.generatemetadata(parameters, None, [], provenancedata)

            filename = resolveoutputfilename(self.filename, parameters, metadata, self, 0, project, None)

            yield None, None, filename, metadata
        else:
            raise Exception("Unable to generate from OutputTemplate, no parent or filename specified")

Response: Generate all relevant input files for this output template.


Instruction:
Explain what the following Python 3 code does
def generatemetadata(self, parameters, parentfile, relevantinputfiles, provenancedata = None):
        assert isinstance(provenancedata,CLAMProvenanceData) or provenancedata is None

        data = {}

        if self.copymetadata:
            #Copy parent metadata
            for key, value in parentfile.metadata.items():
                data[key] = value

        for metafield in self.metafields:
            if isinstance(metafield, ParameterCondition):
                metafield = metafield.evaluate(parameters)
                if not metafield:
                    continue
            assert isinstance(metafield, AbstractMetaField)
            metafield.resolve(data, parameters, parentfile, relevantinputfiles)

        if provenancedata:
            data['provenance'] = provenancedata

        return self.formatclass(None, **data)

Response: Generate metadata given a filename parameters and a dictionary of inputdata


Instruction:
How would you code a function in Python 3 to
return all possible outputtemplates that may occur ( recusrively applied

Response:
def allpossibilities(self):
        """Returns all possible outputtemplates that may occur (recusrively applied)"""
        l = []
        if isinstance(self.then, ParameterCondition):
            #recursive parametercondition
            l += self.then.allpossibilities()
        elif self.then:
            l.append(self.then)
        if self.otherwise:
            if isinstance(self.otherwise, ParameterCondition):
                l += self.otherwise.allpossibilities()
            else:
                l.append(self.otherwise)
        return l


Instruction:
Make a summary of the following Python 3 code
def evaluate(self, parameters):
        if self.match(parameters):
            if isinstance(self.then, ParameterCondition):
                #recursive parametercondition
                return self.then.evaluate(parameters)
            else:
                return self.then
        elif self.otherwise:
            if isinstance(self.otherwise, ParameterCondition):
                #recursive else
                return self.otherwise.evaluate(parameters)
            else:
                return self.otherwise
        return False

Response: Evaluates the condition and returns True if there s no match or whatever the ParameterCondition evaluates to ( recursively applied!


Instruction:
Can you tell what is the following Python 3 function doing
def fromxml(node):
        if not isinstance(node,ElementTree._Element): #pylint: disable=protected-access
            node = parsexmlstring(node)
        assert node.tag.lower() == 'action'

        kwargs = {}
        args = []
        if 'id' in node.attrib:
            kwargs['id'] = node.attrib['id']
        elif 'name' in node.attrib:
            kwargs['name'] = node.attrib['name']
        elif 'description' in node.attrib:
            kwargs['description'] = node.attrib['description']
        elif 'method' in node.attrib:
            kwargs['method'] = node.attrib['method']
        elif 'mimetype' in node.attrib:
            kwargs['mimetype'] = node.attrib['mimetype']
        elif 'allowanonymous' in node.attrib:
            if node.attrib['allowanonymous'] == "yes":
                kwargs['allowanonymous'] = True

        found = False
        for subnode in node:
            if subnode.tag.lower() == 'parametercondition':
                kwargs[node.tag] = ParameterCondition.fromxml(subnode)
            elif subnode.tag in vars(clam.common.parameters):
                args.append(vars(clam.common.parameters)[subnode.tag].fromxml(subnode))
        if not found:
            raise Exception("No condition found in ParameterCondition!")
        return Action(*args, **kwargs)

Response: Static method returning an Action instance from the given XML description. Node can be a string or an etree. _Element.


Instruction:
How would you explain what the following Python 3 function does
def crude_tokenizer(line):
    tokens = []
    buffer = ''
    for c in line.strip():
        if c == ' ' or c in string.punctuation:
            if buffer:
                tokens.append(buffer)
                buffer = ''
        else:
            buffer += c
    if buffer: tokens.append(buffer)
    return tokens

Response: This is a very crude tokenizer from pynlpl


Instruction:
Can you tell what is the following Python 3 function doing
def dicttotext(d, sort=False, max = 0):
    if sort:
        f = lambda x: sorted(x, key=lambda y: -1 * y[1])
    else:
        f = lambda x: x

    output = ""
    for i, (key, value) in enumerate(f(d.items())):
        output += key + "\t" + str(value) + "\n"
        if max != 0 and i >= max:
            break
    return output

Response: Function for converting dictionary to plaintext output optionally with some sort


Instruction:
Write a Python 3 function for
initialising authentication for internal use

Response:
def initauth(self):
        """Initialise authentication, for internal use"""
        headers = {'User-agent': 'CLAMClientAPI-' + clam.common.data.VERSION}
        if self.oauth:
            if not self.oauth_access_token:
                r = requests.get(self.url,headers=headers, verify=self.verify)
                if r.status_code == 404:
                    raise clam.common.data.NotFound("Authorization provider not found")
                elif r.status_code == 403:
                    raise clam.common.data.PermissionDenied("Authorization provider denies access")
                elif not (r.status_code >= 200 and r.status_code <= 299):
                    raise Exception("An error occured, return code " + str(r.status_code))

                data = self._parse(r.text)
                if data is True: #indicates failure
                    raise Exception("No access token provided, but Authorization Provider requires manual user input. Unable to authenticate automatically. Obtain an access token from " + r.geturl())
                else:
                    self.oauth_access_token = data.oauth_access_token

            headers['Authorization'] = 'Bearer ' + self.oauth_access_token
        return headers