Instruction:
Can you create a Python 3 function that
gets the set of plugins that this widget should display.

Response:
def plugins(self):
        """
        Get the set of plugins that this widget should display.
        """
        from fluent_contents import extensions   # Avoid circular reference because __init__.py imports subfolders too
        if self._plugins is None:
            return extensions.plugin_pool.get_plugins()
        else:
            return extensions.plugin_pool.get_plugins_by_name(*self._plugins)


Instruction:
How would you code a function in Python 3 to
return a redirect response for the exception.

Response:
def process_exception(self, request, exception):
        """
        Return a redirect response for the :class:`~fluent_contents.extensions.HttpRedirectRequest`
        """
        if isinstance(exception, HttpRedirectRequest):
            return HttpResponseRedirect(exception.url, status=exception.status)
        else:
            return None


Instruction:
How would you explain what the following Python 3 function does
def render_text(text, language=None):
    # Get the filter
    text_filter = SUPPORTED_LANGUAGES.get(language, None)
    if not text_filter:
        raise ImproperlyConfigured("markup filter does not exist: {0}. Valid options are: {1}".format(
            language, ', '.join(list(SUPPORTED_LANGUAGES.keys()))
        ))

    # Convert.
    return text_filter(text)

Response: Render the text using the template filters provided by Django.


Instruction:
Write a Python 3 script for
reading the derived table data for all objects tracked as remaining.

Response:
def fetch_remaining_instances(self):
        """Read the derived table data for all objects tracked as remaining (=not found in the cache)."""
        if self.remaining_items:
            self.remaining_items = ContentItem.objects.get_real_instances(self.remaining_items)


Instruction:
Implement a Python 3 function for
returning the output in the correct ordering.

Response:
def get_output(self, include_exceptions=False):
        """
        Return the output in the correct ordering.
        :rtype: list[Tuple[contentitem, O]]
        """
        # Order all rendered items in the correct sequence.
        # Don't assume the derived tables are in perfect shape, hence the dict + KeyError handling.
        # The derived tables could be truncated/reset or store_output() could be omitted.
        ordered_output = []
        for item_id in self.output_ordering:
            contentitem = self.item_source[item_id]
            try:
                output = self.item_output[item_id]
            except KeyError:
                # The item was not rendered!
                if not include_exceptions:
                    continue

                output = self.MISSING
            else:
                # Filter exceptions out.
                if not include_exceptions:
                    if isinstance(output, Exception) or output is self.SKIPPED:
                        continue

            ordered_output.append((contentitem, output))

        return ordered_output


Instruction:
Can you generate the documentation for the following Python 3 function
def render_items(self, placeholder, items, parent_object=None, template_name=None, cachable=None):
        # Unless it was done before, disable polymorphic effects.
        is_queryset = False
        if hasattr(items, "non_polymorphic"):
            is_queryset = True
            if not items.polymorphic_disabled and items._result_cache is None:
                items = items.non_polymorphic()

        # See if the queryset contained anything.
        # This test is moved here, to prevent earlier query execution.
        if not items:
            logger.debug("- no items in placeholder '%s'", get_placeholder_debug_name(placeholder))
            return ContentItemOutput(mark_safe(u"<!-- no items in placeholder '{0}' -->".format(escape(get_placeholder_name(placeholder)))), cacheable=True)

        # Tracked data during rendering:
        result = self.result_class(
            request=self.request,
            parent_object=parent_object,
            placeholder=placeholder,
            items=items,
            all_cacheable=self._can_cache_merged_output(template_name, cachable),
        )
        if self.edit_mode:
            result.set_uncachable()

        if is_queryset:
            # Phase 1: get cached output
            self._fetch_cached_output(items, result=result)
            result.fetch_remaining_instances()
        else:
            # The items is either a list of manually created items, or it's a QuerySet.
            # Can't prevent reading the subclasses only, so don't bother with caching here.
            result.add_remaining_list(items)

        # Start the actual rendering of remaining items.
        if result.remaining_items:
            # Phase 2: render remaining items
            self._render_uncached_items(result.remaining_items, result=result)

        # And merge all items together.
        return self.merge_output(result, items, template_name)

Response: Render the items in the given placeholder.


Instruction:
Can you implement a function in Python 3 that
fetches all items from the cache and store them in the result.

Response:
def _fetch_cached_output(self, items, result):
        """
        First try to fetch all items from the cache.
        The items are 'non-polymorphic', so only point to their base class.
        If these are found, there is no need to query the derived data from the database.
        """
        if not appsettings.FLUENT_CONTENTS_CACHE_OUTPUT or not self.use_cached_output:
            result.add_remaining_list(items)
            return

        for contentitem in items:
            result.add_ordering(contentitem)
            output = None

            try:
                plugin = contentitem.plugin
            except PluginNotFound as ex:
                result.store_exception(contentitem, ex)  # Will deal with that later.
                logger.debug("- item #%s has no matching plugin: %s", contentitem.pk, str(ex))
                continue

            # Respect the cache output setting of the plugin
            if self.can_use_cached_output(contentitem):
                result.add_plugin_timeout(plugin)
                output = plugin.get_cached_output(result.placeholder_name, contentitem)

                # Support transition to new output format.
                if output is not None and not isinstance(output, ContentItemOutput):
                    output = None
                    logger.debug("Flushed cached output of {0}#{1} to store new ContentItemOutput format (key: {2})".format(
                        plugin.type_name,
                        contentitem.pk,
                        get_placeholder_name(contentitem.placeholder)
                    ))

            # For debugging, ignore cached values when the template is updated.
            if output and settings.DEBUG:
                cachekey = get_rendering_cache_key(result.placeholder_name, contentitem)
                if is_template_updated(self.request, contentitem, cachekey):
                    output = None

            if output:
                result.store_output(contentitem, output)
            else:
                result.add_remaining(contentitem)


Instruction:
Implement a function in Python 3 to
tell whether the code should try reading cached output for the given contentitem.

Response:
def can_use_cached_output(self, contentitem):
        """
        Tell whether the code should try reading cached output
        """
        plugin = contentitem.plugin
        return appsettings.FLUENT_CONTENTS_CACHE_OUTPUT and plugin.cache_output and contentitem.pk


Instruction:
Can you generate the documentation for the following Python 3 function
def _render_uncached_items(self, items, result):
        for contentitem in items:
            # Render the item.
            # Allow derived classes to skip it.
            try:
                output = self.render_item(contentitem)
            except PluginNotFound as ex:
                result.store_exception(contentitem, ex)
                logger.debug("- item #%s has no matching plugin: %s", contentitem.pk, str(ex))
                continue
            except SkipItem:
                result.set_skipped(contentitem)
                continue

            # Try caching it.
            self._try_cache_output(contentitem, output, result=result)
            if self.edit_mode:
                output.html = markers.wrap_contentitem_output(output.html, contentitem)

            result.store_output(contentitem, output)

Response: Render a list of items that didn t exist in the cache yet.


Instruction:
Can you write a function in Python 3 where it
renders the individual item.

Response:
def render_item(self, contentitem):
        """
        Render the individual item.
        May raise :class:`SkipItem` to ignore an item.
        """
        render_language = get_render_language(contentitem)
        with smart_override(render_language):
            # Plugin output is likely HTML, but it should be placed in mark_safe() to raise awareness about escaping.
            # This is just like Django's Input.render() and unlike Node.render().
            return contentitem.plugin._render_contentitem(self.request, contentitem)


Instruction:
How would you implement a function in Python 3 that
combines all rendered items with a template.

Response:
def merge_output(self, result, items, template_name):
        """
        Combine all rendered items. Allow rendering the items with a template,
        to inserting separators or nice start/end code.
        """
        html_output, media = self.get_html_output(result, items)

        if not template_name:
            merged_html = mark_safe(u''.join(html_output))
        else:
            context = {
                'contentitems': list(zip(items, html_output)),
                'parent_object': result.parent_object,  # Can be None
                'edit_mode': self.edit_mode,
            }

            context = PluginContext(self.request, context)
            merged_html = render_to_string(template_name, context.flatten())

        return ContentItemOutput(merged_html, media, cacheable=result.all_cacheable, cache_timeout=result.all_timeout)


Instruction:
Write a Python 3 function for
collecting all HTML from the rendered items in the correct ordering.

Response:
def get_html_output(self, result, items):
        """
        Collect all HTML from the rendered items, in the correct ordering.
        The media is also collected in the same ordering, in case it's handled by django-compressor for example.
        """
        html_output = []
        merged_media = Media()
        for contentitem, output in result.get_output(include_exceptions=True):
            if output is ResultTracker.MISSING:
                # Likely get_real_instances() didn't return an item for it.
                # The get_real_instances() didn't return an item for the derived table. This happens when either:
                # 1. that table is truncated/reset, while there is still an entry in the base ContentItem table.
                #    A query at the derived table happens every time the page is being rendered.
                # 2. the model was completely removed which means there is also a stale ContentType object.
                class_name = _get_stale_item_class_name(contentitem)
                html_output.append(mark_safe(u"<!-- Missing derived model for ContentItem #{id}: {cls}. -->\n".format(id=contentitem.pk, cls=class_name)))
                logger.warning("Missing derived model for ContentItem #{id}: {cls}.".format(id=contentitem.pk, cls=class_name))
            elif isinstance(output, Exception):
                html_output.append(u'<!-- error: {0} -->\n'.format(str(output)))
            else:
                html_output.append(output.html)
                add_media(merged_media, output.media)

        return html_output, merged_media


Instruction:
Can you tell what is the following Python 3 function doing
def render_placeholder(self, placeholder, parent_object=None, template_name=None, cachable=None, limit_parent_language=True, fallback_language=None):
        placeholder_name = get_placeholder_debug_name(placeholder)
        logger.debug("Rendering placeholder '%s'", placeholder_name)

        # Determine whether the placeholder can be cached.
        cachable = self._can_cache_merged_output(template_name, cachable)
        try_cache = cachable and self.may_cache_placeholders()
        logger.debug("- try_cache=%s cachable=%s template_name=%s", try_cache, cachable, template_name)

        if parent_object is None:
            # To support filtering the placeholders by parent language, the parent object needs to be known.
            # Fortunately, the PlaceholderFieldDescriptor makes sure this doesn't require an additional query.
            parent_object = placeholder.parent

        # Fetch the placeholder output from cache.
        language_code = get_parent_language_code(parent_object)
        cache_key = None
        output = None
        if try_cache:
            cache_key = get_placeholder_cache_key_for_parent(parent_object, placeholder.slot, language_code)
            output = cache.get(cache_key)
            if output:
                logger.debug("- fetched cached output")

        if output is None:
            # Get the items, and render them
            items, is_fallback = self._get_placeholder_items(placeholder, parent_object, limit_parent_language, fallback_language, try_cache)
            output = self.render_items(placeholder, items, parent_object, template_name, cachable)

            if is_fallback:
                # Caching fallbacks is not supported yet,
                # content could be rendered in a different gettext language domain.
                output.cacheable = False

            # Store the full-placeholder contents in the cache.
            if try_cache and output.cacheable:
                if output.cache_timeout is not DEFAULT_TIMEOUT:
                    # The timeout is based on the minimal timeout used in plugins.
                    cache.set(cache_key, output, output.cache_timeout)
                else:
                    # Don't want to mix into the default 0/None issue.
                    cache.set(cache_key, output)

        return output

Response: Render a single placeholder.


Instruction:
Write a Python 3 function that can
register a plugin with the CMS.

Response:
def register(self, plugin):
        """
        Make a plugin known to the CMS.

        :param plugin: The plugin class, deriving from :class:`ContentPlugin`.
        :type plugin: :class:`ContentPlugin`

        The plugin will be instantiated once, just like Django does this with :class:`~django.contrib.admin.ModelAdmin` classes.
        If a plugin is already registered, this will raise a :class:`PluginAlreadyRegistered` exception.
        """
        # Duck-Typing does not suffice here, avoid hard to debug problems by upfront checks.
        assert issubclass(plugin, ContentPlugin), "The plugin must inherit from `ContentPlugin`"
        assert plugin.model, "The plugin has no model defined"
        assert issubclass(plugin.model, ContentItem), "The plugin model must inherit from `ContentItem`"
        assert issubclass(plugin.form, ContentItemForm), "The plugin form must inherit from `ContentItemForm`"

        name = plugin.__name__  # using class here, no instance created yet.
        name = name.lower()
        if name in self.plugins:
            raise PluginAlreadyRegistered("{0}: a plugin with this name is already registered".format(name))

        # Avoid registering 2 plugins to the exact same model. If you want to reuse code, use proxy models.
        if plugin.model in self._name_for_model:
            # Having 2 plugins for one model breaks ContentItem.plugin and the frontend code
            # that depends on using inline-model names instead of plugins. Good luck fixing that.
            # Better leave the model==plugin dependency for now.
            existing_plugin = self.plugins[self._name_for_model[plugin.model]]
            raise ModelAlreadyRegistered("Can't register the model {0} to {2}, it's already registered to {1}!".format(
                plugin.model.__name__,
                existing_plugin.name,
                plugin.__name__
            ))

        # Make a single static instance, similar to ModelAdmin.
        plugin_instance = plugin()
        self.plugins[name] = plugin_instance
        self._name_for_model[plugin.model] = name       # Track reverse for model.plugin link

        # Only update lazy indexes if already created
        if self._name_for_ctype_id is not None:
            self._name_for_ctype_id[plugin.type_id] = name

        return plugin


Instruction:
How would you explain what the following Python 3 function does
def get_allowed_plugins(self, placeholder_slot):
        # See if there is a limit imposed.
        slot_config = appsettings.FLUENT_CONTENTS_PLACEHOLDER_CONFIG.get(placeholder_slot) or {}
        plugins = slot_config.get('plugins')
        if not plugins:
            return self.get_plugins()
        else:
            try:
                return self.get_plugins_by_name(*plugins)
            except PluginNotFound as e:
                raise PluginNotFound(str(e) + " Update the plugin list of the FLUENT_CONTENTS_PLACEHOLDER_CONFIG['{0}'] setting.".format(placeholder_slot))

Response: Return the plugins which are supported in the given placeholder name.


Instruction:
Create a Python 3 function for
returning a list of plugins by name or class name.

Response:
def get_plugins_by_name(self, *names):
        """
        Return a list of plugins by plugin class, or name.
        """
        self._import_plugins()
        plugin_instances = []
        for name in names:
            if isinstance(name, six.string_types):
                try:
                    plugin_instances.append(self.plugins[name.lower()])
                except KeyError:
                    raise PluginNotFound("No plugin named '{0}'.".format(name))
            elif isinstance(name, type) and issubclass(name, ContentPlugin):
                # Will also allow classes instead of strings.
                plugin_instances.append(self.plugins[self._name_for_model[name.model]])
            else:
                raise TypeError("get_plugins_by_name() expects a plugin name or class, not: {0}".format(name))
        return plugin_instances


Instruction:
Create a Python 3 function for
returning all model classes which are exposed by plugins.

Response:
def get_model_classes(self):
        """
        Return all :class:`~fluent_contents.models.ContentItem` model classes which are exposed by plugins.
        """
        self._import_plugins()
        return [plugin.model for plugin in self.plugins.values()]


Instruction:
Given the following Python 3 function, write the documentation
def get_plugin_by_model(self, model_class):
        self._import_plugins()                       # could happen during rendering that no plugin scan happened yet.
        assert issubclass(model_class, ContentItem)  # avoid confusion between model instance and class here!

        try:
            name = self._name_for_model[model_class]
        except KeyError:
            raise PluginNotFound("No plugin found for model '{0}'.".format(model_class.__name__))
        return self.plugins[name]

Response: Returns the corresponding plugin for a given model class.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def _import_plugins(self):
        if self.detected:
            return

        # In some cases, plugin scanning may start during a request.
        # Make sure there is only one thread scanning for plugins.
        self.scanLock.acquire()
        if self.detected:
            return  # previous threaded released + completed

        try:
            import_apps_submodule("content_plugins")
            self.detected = True
        finally:
            self.scanLock.release()

Response: Internal function to import all plugin packages.


Instruction:
Write a Python 3 script for
applying all filters for a given HTML snippet.

Response:
def apply_filters(instance, html, field_name):
    """
    Run all filters for a given HTML snippet.
    Returns the results of the pre-filter and post-filter as tuple.

    This function can be called from the :meth:`~django.db.models.Model.full_clean` method in the model.
    That function is called when the form values are assigned.
    For example:

    .. code-block:: python

        def full_clean(self, *args, **kwargs):
            super(TextItem, self).full_clean(*args, **kwargs)
            self.html, self.html_final = apply_filters(self, self.html, field_name='html')

    :type instance: fluent_contents.models.ContentItem
    :raise ValidationError: when one of the filters detects a problem.
    """
    try:
        html = apply_pre_filters(instance, html)

        # Perform post processing. This does not effect the original 'html'
        html_final = apply_post_filters(instance, html)
    except ValidationError as e:
        if hasattr(e, 'error_list'):
            # The filters can raise a "dump" ValidationError with a single error.
            # However, during post_clean it's expected that the fields are named.
            raise ValidationError({
                field_name: e.error_list
            })
        raise

    return html, html_final


Instruction:
Given the following Python 3 function, write the documentation
def apply_pre_filters(instance, html):
    # Allow pre processing. Typical use-case is HTML syntax correction.
    for post_func in appsettings.PRE_FILTER_FUNCTIONS:
        html = post_func(instance, html)

    return html

Response: Apply pre filters to the HTML source code.


Instruction:
Implement a function in Python 3 to
apply post processing functions to the given HTML.

Response:
def apply_post_filters(instance, html):
    """
    Allow post processing functions to change the text.
    This change is not saved in the original text.

    :type instance: fluent_contents.models.ContentItem
    :raise ValidationError: when one of the filters detects a problem.
    """
    for post_func in appsettings.POST_FILTER_FUNCTIONS:
        html = post_func(instance, html)

    return html


Instruction:
Explain what the following Python 3 code does
def clear_commentarea_cache(comment):
    parent = comment.content_object
    for instance in CommentsAreaItem.objects.parent(parent):
        instance.clear_cache()

Response: Clean the plugin output cache of a rendered plugin.


Instruction:
Given the following Python 3 function, write the documentation
def parent_site(self, site):
        # Avoid auto filter if site is already set.
        self._parent_site = site

        if sharedcontent_appsettings.FLUENT_SHARED_CONTENT_ENABLE_CROSS_SITE:
            # Allow content to be shared between all sites:
            return self.filter(Q(parent_site=site) | Q(is_cross_site=True))
        else:
            return self.filter(parent_site=site)

Response: Filter to the given site only give content relevant for that site.


Instruction:
How would you implement a function in Python 3 that
returns a single site instance.

Response:
def _single_site(self):
        """
        Make sure the queryset is filtered on a parent site, if that didn't happen already.
        """
        if appsettings.FLUENT_CONTENTS_FILTER_SITE_ID and self._parent_site is None:
            return self.parent_site(settings.SITE_ID)
        else:
            return self


Instruction:
Given the following Python 3 function, write the documentation
def get_template_placeholder_data(template):
    # Find the instances.
    nodes = get_node_instances(template, PagePlaceholderNode)

    # Avoid duplicates, wrap in a class.
    names = set()
    result = []
    for pageplaceholdernode in nodes:
        data = PlaceholderData(
            slot=pageplaceholdernode.get_slot(),
            title=pageplaceholdernode.get_title(),
            role=pageplaceholdernode.get_role(),
            fallback_language=pageplaceholdernode.get_fallback_language(),
        )

        if data.slot not in names:
            result.append(data)
            names.add(data.slot)

    return result

Response: Returns the list of PlaceholderData objects found in a template.


Instruction:
Here you have a function in Python 3, explain what it does
def inspect_model(self, model):
        # See which interesting fields the model holds.
        url_fields = sorted(f for f in model._meta.fields if isinstance(f, (PluginUrlField, models.URLField)))
        file_fields = sorted(f for f in model._meta.fields if isinstance(f, (PluginImageField, models.FileField)))
        html_fields = sorted(f for f in model._meta.fields if isinstance(f, (models.TextField, PluginHtmlField)))
        all_fields = [f.name for f in (file_fields + html_fields + url_fields)]
        if not all_fields:
            return []

        if model.__name__ in self.exclude:
            self.stderr.write("Skipping {0} ({1})\n".format(model.__name__, ", ".join(all_fields)))
            return []

        sys.stderr.write("Inspecting {0} ({1})\n".format(model.__name__, ", ".join(all_fields)))

        q_notnull = reduce(operator.or_, (Q(**{"{0}__isnull".format(f): False}) for f in all_fields))
        qs = model.objects.filter(q_notnull).order_by('pk')

        urls = []
        for object in qs:
            # HTML fields need proper html5lib parsing
            for field in html_fields:
                value = getattr(object, field.name)
                if value:
                    html_images = self.extract_html_urls(value)
                    urls += html_images

                    for image in html_images:
                        self.show_match(object, image)

            # Picture fields take the URL from the storage class.
            for field in file_fields:
                value = getattr(object, field.name)
                if value:
                    value = unquote_utf8(value.url)
                    urls.append(value)
                    self.show_match(object, value)

            # URL fields can be read directly.
            for field in url_fields:
                value = getattr(object, field.name)
                if value:
                    if isinstance(value, six.text_type):
                        value = force_text(value)
                    else:
                        value = value.to_db_value()  # AnyUrlValue

                    urls.append(value)
                    self.show_match(object, value)
        return urls

Response: Inspect a single model.


Instruction:
How would you implement a function in Python 3 that
takes all <img src =.. srcset =.. srcset =.. srcset =.. srcset =.. srcset =.. srcset =.. srcset =.. srcset =.. srcset =.. srcset =.. srcset =.. srcset =.. srcset =.. srcset =.. srcset =.. srcset =.. srcset =.. srcset =.. srcset =.. srcset =.. srcset =.. srcset =.. srcset =

Response:
def extract_html_urls(self, html):
        """
        Take all ``<img src="..">`` from the HTML
        """
        p = HTMLParser(tree=treebuilders.getTreeBuilder("dom"))
        dom = p.parse(html)
        urls = []

        for img in dom.getElementsByTagName('img'):
            src = img.getAttribute('src')
            if src:
                urls.append(unquote_utf8(src))

            srcset = img.getAttribute('srcset')
            if srcset:
                urls += self.extract_srcset(srcset)

        for source in dom.getElementsByTagName('source'):
            srcset = source.getAttribute('srcset')
            if srcset:
                urls += self.extract_srcset(srcset)

        for source in dom.getElementsByTagName('a'):
            href = source.getAttribute('href')
            if href:
                urls.append(unquote_utf8(href))

        return urls


Instruction:
Implement a function in Python 3 to
extract the set of images from a source set.

Response:
def extract_srcset(self, srcset):
        """
        Handle ``srcset="image.png 1x, image@2x.jpg 2x"``
        """
        urls = []
        for item in srcset.split(','):
            if item:
                urls.append(unquote_utf8(item.rsplit(' ', 1)[0]))
        return urls


Instruction:
Write a Python 3 function for
deleting the translation from the ContentItems.

Response:
def on_delete_model_translation(instance, **kwargs):
    """
    Make sure ContentItems are deleted when a translation in deleted.
    """
    translation = instance

    parent_object = translation.master
    parent_object.set_current_language(translation.language_code)

    # Also delete any associated plugins
    # Placeholders are shared between languages, so these are not affected.
    for item in ContentItem.objects.parent(parent_object, limit_parent_language=True):
        item.delete()


Instruction:
Create a Python 3 function to
take an HTML fragment and processes it using html5lib to ensure that the HTML is well - formed.

Response:
def clean_html(input, sanitize=False):
    """
    Takes an HTML fragment and processes it using html5lib to ensure that the HTML is well-formed.

    :param sanitize: Remove unwanted HTML tags and attributes.

    >>> clean_html("<p>Foo<b>bar</b></p>")
    u'<p>Foo<b>bar</b></p>'
    >>> clean_html("<p>Foo<b>bar</b><i>Ooops!</p>")
    u'<p>Foo<b>bar</b><i>Ooops!</i></p>'
    >>> clean_html('<p>Foo<b>bar</b>& oops<a href="#foo&bar">This is a <>link</a></p>')
    u'<p>Foo<b>bar</b>&amp; oops<a href=#foo&amp;bar>This is a &lt;&gt;link</a></p>'
    """
    parser_kwargs = {}
    serializer_kwargs = {}
    if sanitize:
        if HTMLSanitizer is None:
            # new syntax as of 0.99999999/1.0b9 (Released on July 14, 2016)
            serializer_kwargs['sanitize'] = True
        else:
            parser_kwargs['tokenizer'] = HTMLSanitizer

    p = HTMLParser(tree=treebuilders.getTreeBuilder("dom"), **parser_kwargs)
    dom_tree = p.parseFragment(input)
    walker = treewalkers.getTreeWalker("dom")
    stream = walker(dom_tree)

    s = HTMLSerializer(omit_optional_tags=False, **serializer_kwargs)
    return "".join(s.serialize(stream))


Instruction:
Create a Python 3 function to
return the contents as a dictionary for client - side export.

Response:
def as_dict(self):
        """
        Return the contents as dictionary, for client-side export.
        The dictionary contains the fields:

        * ``slot``
        * ``title``
        * ``role``
        * ``fallback_language``
        * ``allowed_plugins``
        """
        plugins = self.get_allowed_plugins()
        return {
            'slot': self.slot,
            'title': self.title,
            'role': self.role,
            'fallback_language': self.fallback_language,
            'allowed_plugins': [plugin.name for plugin in plugins],
        }


Instruction:
Implement a Python 3 function for
forwarding the MarkupItem model to the correct proxy model that implements the given language.

Response:
def _forwards(apps, schema_editor):
    """
    Make sure that the MarkupItem model actually points
    to the correct proxy model, that implements the given language.
    """
    # Need to work on the actual models here.
    from fluent_contents.plugins.markup.models import LANGUAGE_MODEL_CLASSES
    from fluent_contents.plugins.markup.models import MarkupItem
    from django.contrib.contenttypes.models import ContentType

    ctype = ContentType.objects.get_for_model(MarkupItem)

    for language, proxy_model in LANGUAGE_MODEL_CLASSES.items():
        proxy_ctype = ContentType.objects.get_for_model(proxy_model, for_concrete_model=False)
        MarkupItem.objects.filter(
            polymorphic_ctype=ctype, language=language
        ).update(
            polymorphic_ctype=proxy_ctype
        )


Instruction:
Can you generate the documentation for the following Python 3 function
def get_formset(self, request, obj=None, **kwargs):
        def _placeholder_initial(p):
            # p.as_dict() returns allowed_plugins too for the client-side API.
            return {
                'slot': p.slot,
                'title': p.title,
                'role': p.role,
            }

        # Note this method is called twice, the second time in get_fieldsets() as `get_formset(request).form`
        initial = []
        if request.method == "GET":
            placeholder_admin = self._get_parent_modeladmin()

            # Grab the initial data from the parent PlaceholderEditorBaseMixin
            data = placeholder_admin.get_placeholder_data(request, obj)
            initial = [_placeholder_initial(d) for d in data]

            # Order initial properly,

        # Inject as default parameter to the constructor
        # This is the BaseExtendedGenericInlineFormSet constructor
        FormSetClass = super(PlaceholderEditorInline, self).get_formset(request, obj, **kwargs)
        FormSetClass.__init__ = curry(FormSetClass.__init__, initial=initial)
        return FormSetClass

Response: Returns a FormSet subclass with initial data for the current object.


Instruction:
Can you tell what is the following Python 3 function doing
def get_inline_instances(self, request, *args, **kwargs):
        inlines = super(PlaceholderEditorAdmin, self).get_inline_instances(request, *args, **kwargs)

        extra_inline_instances = []
        inlinetypes = self.get_extra_inlines()
        for InlineType in inlinetypes:
            inline_instance = InlineType(self.model, self.admin_site)
            extra_inline_instances.append(inline_instance)

        return extra_inline_instances + inlines

Response: Create the inlines for the admin including the placeholder and contentitem inlines.


Instruction:
Write a Python 3 function for
returning the placeholder data as dictionary.

Response:
def get_placeholder_data_view(self, request, object_id):
        """
        Return the placeholder data as dictionary.
        This is used in the client for the "copy" functionality.
        """
        language = 'en'  #request.POST['language']
        with translation.override(language):  # Use generic solution here, don't assume django-parler is used now.
            obj = self.get_object(request, object_id)

        if obj is None:
            json = {'success': False, 'error': 'Page not found'}
            status = 404
        elif not self.has_change_permission(request, obj):
            json = {'success': False, 'error': 'No access to page'}
            status = 403
        else:
            # Fetch the forms that would be displayed,
            # return the data as serialized form data.
            status = 200
            json = {
                'success': True,
                'object_id': object_id,
                'language_code': language,
                'formset_forms': self._get_object_formset_data(request, obj),
            }

        return JsonResponse(json, status=status)


Instruction:
Make a summary of the following Python 3 code
def get_args(method_or_func):
    try:
        # Python 3.0+
        args = list(inspect.signature(method_or_func).parameters.keys())
    except AttributeError:
        # Python 2.7
        args = inspect.getargspec(method_or_func).args
    return args

Response: Returns method or function arguments.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def jwt_optional(fn):
    @wraps(fn)
    def wrapper(*args, **kwargs):
        try:
            jwt_data = _decode_jwt_from_headers()
            ctx_stack.top.jwt = jwt_data
        except (NoAuthorizationError, InvalidHeaderError):
            pass
        return fn(*args, **kwargs)
    return wrapper

Response: Decorator that will decorate a view function with this decorator that will check the request for a valid JWT and put it into the application context.


Instruction:
Can you generate the documentation for the following Python 3 function
def decode_jwt(encoded_token):
    secret = config.decode_key
    algorithm = config.algorithm
    audience = config.audience
    return jwt.decode(encoded_token, secret, algorithms=[algorithm], audience=audience)

Response: Decodes a JWT encoded string into a base64 - encoded token.


Instruction:
Given the following Python 3 function, write the documentation
def init_app(self, app):
        # Save this so we can use it later in the extension
        if not hasattr(app, 'extensions'):   # pragma: no cover
            app.extensions = {}
        app.extensions['flask-jwt-simple'] = self

        # Set all the default configurations for this extension
        self._set_default_configuration_options(app)
        self._set_error_handler_callbacks(app)

        # Set propagate exceptions, so all of our error handlers properly
        # work in production
        app.config['PROPAGATE_EXCEPTIONS'] = True

Response: Register this extension with the flask app


Instruction:
Write a Python 3 function for
setting the error handler callbacks used by this extension.

Response:
def _set_error_handler_callbacks(self, app):
        """
        Sets the error handler callbacks used by this extension
        """
        @app.errorhandler(NoAuthorizationError)
        def handle_no_auth_error(e):
            return self._unauthorized_callback(str(e))

        @app.errorhandler(InvalidHeaderError)
        def handle_invalid_header_error(e):
            return self._invalid_token_callback(str(e))

        @app.errorhandler(jwt.ExpiredSignatureError)
        def handle_expired_error(e):
            return self._expired_token_callback()

        @app.errorhandler(jwt.InvalidTokenError)
        def handle_invalid_token_error(e):
            return self._invalid_token_callback(str(e))


Instruction:
Explain what the following Python 3 code does
def _set_default_configuration_options(app):
        # Options for JWTs when the TOKEN_LOCATION is headers
        app.config.setdefault('JWT_HEADER_NAME', 'Authorization')
        app.config.setdefault('JWT_HEADER_TYPE', 'Bearer')

        # How long an a token created with 'create_jwt' will last before
        # it expires (when using the default jwt_data_callback function).
        app.config.setdefault('JWT_EXPIRES', datetime.timedelta(hours=1))

        # What algorithm to use to sign the token. See here for a list of options:
        # https://github.com/jpadilla/pyjwt/blob/master/jwt/api_jwt.py
        app.config.setdefault('JWT_ALGORITHM', 'HS256')

        # Key that acts as the identity for the JWT
        app.config.setdefault('JWT_IDENTITY_CLAIM', 'sub')

        # Expected value of the audience claim
        app.config.setdefault('JWT_DECODE_AUDIENCE', None)

        # Secret key to sign JWTs with. Only used if a symmetric algorithm is
        # used (such as the HS* algorithms).
        app.config.setdefault('JWT_SECRET_KEY', None)

        # Keys to sign JWTs with when use when using an asymmetric
        # (public/private key) algorithms, such as RS* or EC*
        app.config.setdefault('JWT_PRIVATE_KEY', None)
        app.config.setdefault('JWT_PUBLIC_KEY', None)

Response: Sets the default configuration options used by this extension.


Instruction:
Make a summary of the following Python 3 code
def releases(release_id=None, **kwargs):
    if not 'id' in kwargs and release_id is not None:
        kwargs['release_id'] = release_id
        return Fred().release(**kwargs)
    return Fred().releases(**kwargs)

Response: Get all releases of economic data.


Instruction:
How would you implement a function in Python 3 that
gets an economic data series.

Response:
def series(identifier=None, **kwargs):
    """Get an economic data series."""
    if identifier:
        kwargs['series_id'] = identifier
    if 'release' in kwargs:
        kwargs.pop('release')
        path = 'release'
    elif 'releases' in kwargs:
        kwargs.pop('releases')
        path = 'release'
    else:
        path = None
    return Fred().series(path, **kwargs)


Instruction:
Can you tell what is the following Python 3 function doing
def source(source_id=None, **kwargs):
    if source_id is not None:
        kwargs['source_id'] = source_id
    elif 'id' in kwargs:
        source_id = kwargs.pop('id')
        kwargs['source_id'] = source_id
    if 'releases' in kwargs:
        kwargs.pop('releases')
        path = 'releases'
    else:
        path = None
    return Fred().source(path, **kwargs)

Response: Get a source of economic data.


Instruction:
Create a Python 3 function for
getting the sources of economic data.

Response:
def sources(source_id=None, **kwargs):
    """Get the sources of economic data."""
    if source_id or 'id' in kwargs:
        return source(source_id, **kwargs)
    return Fred().sources(**kwargs)


Instruction:
Write a Python 3 function that can
create the URL path with the Fred endpoint and given arguments.

Response:
def _create_path(self, *args):
        """Create the URL path with the Fred endpoint and given arguments."""
        args = filter(None, args)
        path = self.endpoint + '/'.join(args)
        return path


Instruction:
How would you implement a function in Python 3 that
performs a GET request againt the Fred API endpoint.

Response:
def get(self, *args, **kwargs):
        """Perform a GET request againt the Fred API endpoint."""
        location = args[0]
        params = self._get_keywords(location, kwargs)
        url = self._create_path(*args)
        request = requests.get(url, params=params)
        content = request.content
        self._request = request
        return self._output(content)


Instruction:
Given the following Python 3 function, write the documentation
def _get_keywords(self, location, keywords):
        if 'xml' in keywords:
            keywords.pop('xml')
            self.xml = True
        else:
            keywords['file_type'] = 'json'
        if 'id' in keywords:
            if location != 'series':
                location = location.rstrip('s')
            key = '%s_id' % location
            value = keywords.pop('id')
            keywords[key] = value
        if 'start' in keywords:
            time = keywords.pop('start')
            keywords['realtime_start'] = time
        if 'end' in keywords:
            time = keywords.pop('end')
            keywords['realtime_end'] = time
        if 'sort' in keywords:
            order = keywords.pop('sort')
            keywords['sort_order'] = order
        keywords['api_key'] = self.api_key
        return keywords

Response: Format GET request s parameters from keywords.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def item_extra_kwargs(self, item):
        if use_feed_image:
            feed_image = item.feed_image
            if feed_image:
                image_complete_url = urljoin(
                    self.get_site_url(), feed_image.file.url
                )
            else:
                image_complete_url = ""

        content_field = getattr(item, self.item_content_field)
        try:
            content = expand_db_html(content_field)
        except:
            content = content_field.__html__()

        soup = BeautifulSoup(content, 'html.parser')
        # Remove style attribute to remove large botton padding
        for div in soup.find_all("div", {'class': 'responsive-object'}):
            del div['style']
        # Add site url to image source
        for img_tag in soup.findAll('img'):
            if img_tag.has_attr('src'):
                img_tag['src'] = urljoin(self.get_site_url(), img_tag['src'])

        fields_to_add = {
            'content': soup.prettify(formatter="html"),
        }

        if use_feed_image:
            fields_to_add['image'] = image_complete_url
        else:
            fields_to_add['image'] = ""

        return fields_to_add

Response:
Returns a dictionary that is used with
            the add_item call of the custom feed generator.


Instruction:
Here you have a function in Python 3, explain what it does
def treenav_undefined_url(request, item_slug):
    item = get_object_or_404(treenav.MenuItem, slug=item_slug)  # noqa
    # do something with item here and return an HttpResponseRedirect
    raise Http404

Response: This is a sample view that can be used to provide custom handlers for undefined menu items on a per - item basis.


Instruction:
How would you explain what the following Python 3 function does
def refresh_hrefs(self, request):
        for item in treenav.MenuItem.objects.all():
            item.save()  # refreshes the HREF
        self.message_user(request, _('Menu item HREFs refreshed successfully.'))
        info = self.model._meta.app_label, self.model._meta.model_name
        changelist_url = reverse('admin:%s_%s_changelist' % info, current_app=self.admin_site.name)
        return redirect(changelist_url)

Response: Refresh all the cached menu item HREFs in the database.


Instruction:
Can you create a Python 3 function that
removes all MenuItems from Cache.

Response:
def clean_cache(self, request):
        """
        Remove all MenuItems from Cache.
        """
        treenav.delete_cache()
        self.message_user(request, _('Cache menuitem cache cleaned successfully.'))
        info = self.model._meta.app_label, self.model._meta.model_name
        changelist_url = reverse('admin:%s_%s_changelist' % info, current_app=self.admin_site.name)
        return redirect(changelist_url)


Instruction:
Can you generate the documentation for the following Python 3 function
def rebuild_tree(self, request):
        '''
        Rebuilds the tree and clears the cache.
        '''
        self.model.objects.rebuild()
        self.message_user(request, _('Menu Tree Rebuilt.'))
        return self.clean_cache(request)

Response: Rebuilds the tree and clears the cache.


Instruction:
Create a Python 3 function to
rebuild the tree after saving items related to parent.

Response:
def save_related(self, request, form, formsets, change):
        """
        Rebuilds the tree after saving items related to parent.
        """
        super(MenuItemAdmin, self).save_related(request, form, formsets, change)
        self.model.objects.rebuild()


Instruction:
Given the following Python 3 function, write the documentation
def _calculate_dispersion(X: Union[pd.DataFrame, np.ndarray], labels: np.ndarray, centroids: np.ndarray) -> float:
        disp = np.sum(np.sum([np.abs(inst - centroids[label]) ** 2 for inst, label in zip(X, labels)]))  # type: float
        return disp

Response: Calculate the dispersion between actual points and their assigned centroids


Instruction:
How would you implement a function in Python 3 that
calculates the gap value of the given data n_refs and n_clusters and return the resutling gap value and n_clusters.

Response:
def _calculate_gap(self, X: Union[pd.DataFrame, np.ndarray], n_refs: int, n_clusters: int) -> Tuple[float, int]:
        """
        Calculate the gap value of the given data, n_refs, and number of clusters.
        Return the resutling gap value and n_clusters
        """
        # Holder for reference dispersion results
        ref_dispersions = np.zeros(n_refs)  # type: np.ndarray

        # For n_references, generate random sample and perform kmeans getting resulting dispersion of each loop
        for i in range(n_refs):

            # Create new random reference set
            random_data = np.random.random_sample(size=X.shape)  # type: np.ndarray

            # Fit to it, getting the centroids and labels, and add to accumulated reference dispersions array.
            centroids, labels = kmeans2(data=random_data,
                                        k=n_clusters,
                                        iter=10,
                                        minit='points')  # type: Tuple[np.ndarray, np.ndarray]
            dispersion = self._calculate_dispersion(X=random_data, labels=labels, centroids=centroids)  # type: float
            ref_dispersions[i] = dispersion

        # Fit cluster to original data and create dispersion calc.
        centroids, labels = kmeans2(data=X, k=n_clusters, iter=10, minit='points')
        dispersion = self._calculate_dispersion(X=X, labels=labels, centroids=centroids)

        # Calculate gap statistic
        gap_value = np.mean(np.log(ref_dispersions)) - np.log(dispersion)

        return gap_value, int(n_clusters)


Instruction:
Given the following Python 3 function, write the documentation
def _process_with_rust(self, X: Union[pd.DataFrame, np.ndarray], n_refs: int, cluster_array: np.ndarray):
        from gap_statistic.rust import gapstat
        for label, gap_value in gapstat.optimal_k(X, list(cluster_array)):
            yield (gap_value, label)

Response: Process gap stat using pure rust


Instruction:
Given the following Python 3 function, write the documentation
def _process_with_joblib(self, X: Union[pd.DataFrame, np.ndarray], n_refs: int, cluster_array: np.ndarray):
        if Parallel is None:
            raise EnvironmentError('joblib is not installed; cannot use joblib as the parallel backend!')

        with Parallel(n_jobs=self.n_jobs) as parallel:
            for gap_value, n_clusters in parallel(delayed(self._calculate_gap)(X, n_refs, n_clusters)
                                                  for n_clusters in cluster_array):
                yield (gap_value, n_clusters)

Response: Process calling of. calculate_gap method using joblib.


Instruction:
Here you have a function in Python 3, explain what it does
def _process_with_multiprocessing(self, X: Union[pd.DataFrame, np.ndarray], n_refs: int, cluster_array: np.ndarray):
        with ProcessPoolExecutor(max_workers=self.n_jobs) as executor:

            jobs = [executor.submit(self._calculate_gap, X, n_refs, n_clusters)
                    for n_clusters in cluster_array
                    ]

            for future in as_completed(jobs):
                gap_value, k = future.result()
                yield (gap_value, k)

Response: Process calling of. calculate_gap method using the multiprocessing library.


Instruction:
Make a summary of the following Python 3 code
def _process_non_parallel(self, X: Union[pd.DataFrame, np.ndarray], n_refs: int, cluster_array: np.ndarray):
        for gap_value, n_clusters in [self._calculate_gap(X, n_refs, n_clusters)
                                      for n_clusters in cluster_array]:
            yield (gap_value, n_clusters)

Response: Process calling of. calculate_gap method using no parallel backend ; simple for loop


Instruction:
Create a Python 3 function for
getting the last migration batch.

Response:
def get_last(self):
        """
        Get the last migration batch.

        :rtype: list
        """
        query = self.table().where('batch', self.get_last_batch_number())

        return query.order_by('migration', 'desc').get()


Instruction:
Can you tell what is the following Python 3 function doing
def compile_insert(self, query, values):
        # Essentially we will force every insert to be treated as a batch insert which
        # simply makes creating the SQL easier for us since we can utilize the same
        # basic routine regardless of an amount of records given to us to insert.
        table = self.wrap_table(query.from__)

        if not isinstance(values, list):
            values = [values]

        columns = self.columnize(values[0].keys())

        # We need to build a list of parameter place-holders of values that are bound
        # to the query. Each insert should have the exact same amount of parameter
        # bindings so we can just go off the first list of values in this array.
        parameters = self.parameterize(values[0].values())

        value = ['(%s)' % parameters] * len(values)

        parameters = ', '.join(value)

        return 'INSERT INTO %s (%s) VALUES %s' % (table, columns, parameters)

Response: Compile an insert SQL statement for the given values.


Instruction:
How would you explain what the following Python 3 function does
def get_alter_table_sql(self, diff):
        sql = []

        for column_diff in diff.changed_columns.values():
            if self.is_unchanged_binary_column(column_diff):
                continue

            old_column_name = column_diff.old_column_name
            column = column_diff.column

            if any([column_diff.has_changed('type'),
                    column_diff.has_changed('precision'),
                    column_diff.has_changed('scale'),
                    column_diff.has_changed('fixed')]):
                query = 'ALTER ' + old_column_name + ' TYPE ' + self.get_sql_type_declaration(column.to_dict())
                sql.append('ALTER TABLE ' + diff.name + ' ' + query)

            if column_diff.has_changed('default') or column_diff.has_changed('type'):
                if column.get_default() is None:
                    default_clause = ' DROP DEFAULT'
                else:
                    default_clause = ' SET' + self.get_default_value_declaration_sql(column.to_dict())

                query = 'ALTER ' + old_column_name + default_clause
                sql.append('ALTER TABLE ' + diff.name + ' ' + query)

            if column_diff.has_changed('notnull'):
                op = 'DROP'
                if column.get_notnull():
                    op = 'SET'

                query = 'ALTER ' + old_column_name + ' ' + op + ' NOT NULL'
                sql.append('ALTER TABLE ' + diff.name + ' ' + query)

            if column_diff.has_changed('autoincrement'):
                if column.get_autoincrement():
                    seq_name = self.get_identity_sequence_name(diff.name, old_column_name)

                    sql.append('CREATE SEQUENCE ' + seq_name)
                    sql.append('SELECT setval(\'' + seq_name + '\', '
                               '(SELECT MAX(' + old_column_name + ') FROM ' + diff.name + '))')
                    query = 'ALTER ' + old_column_name + ' SET DEFAULT nextval(\'' + seq_name + '\')'
                    sql.append('ALTER TABLE ' + diff.name + ' ' + query)
                else:
                    query = 'ALTER ' + old_column_name + ' DROP DEFAULT'
                    sql.append('ALTER TABLE ' + diff.name + ' ' + query)

            if column_diff.has_changed('length'):
                query = 'ALTER ' + old_column_name + ' TYPE ' + self.get_sql_type_declaration(column.to_dict())
                sql.append('ALTER TABLE ' + diff.name + ' ' + query)

        for old_column_name, column in diff.renamed_columns.items():
            sql.append('ALTER TABLE ' + diff.name + ' '
                       'RENAME COLUMN ' + old_column_name + ' TO ' + column.get_name())

        return sql

Response: Get the ALTER TABLE SQL statement for the table diff.


Instruction:
Can you tell what is the following Python 3 function doing
def _date_based_where(self, type, query, where):
        value = str(where['value']).zfill(2)
        value = self.parameter(value)

        return 'strftime(\'%s\', %s) %s %s'\
               % (type, self.wrap(where['column']),
                  where['operator'], value)

Response: Compile a date based where clause


Instruction:
Can you tell what is the following Python 3 function doing
def compile_select(self, query):
        sql = super(MySqlQueryGrammar, self).compile_select(query)

        if query.unions:
            sql = '(%s) %s' % (sql, self._compile_unions(query))

        return sql

Response: Compile a select query into SQL


Instruction:
Write a Python 3 function for
compiling the lock into SQL

Response:
def _compile_lock(self, query, value):
        """
        Compile the lock into SQL

        :param query: A QueryBuilder instance
        :type query: QueryBuilder

        :param value: The lock value
        :type value: bool or str

        :return: The compiled lock
        :rtype: str
        """
        if isinstance(value, basestring):
            return value

        if value is True:
            return 'FOR UPDATE'
        elif value is False:
            return 'LOCK IN SHARE MODE'


Instruction:
Can you generate the documentation for the following Python 3 function
def compile_update(self, query, values):
        sql = super(MySqlQueryGrammar, self).compile_update(query, values)

        if query.orders:
            sql += ' %s' % self._compile_orders(query, query.orders)

        if query.limit_:
            sql += ' %s' % self._compile_limit(query, query.limit_)

        return sql.rstrip()

Response: Compile an update statement into SQL


Instruction:
Can you tell what is the following Python 3 function doing
def collapse(self):
        results = []

        if isinstance(self._items, dict):
            items = self._items.values()

        for values in items:
            if isinstance(values, Collection):
                values = values.all()

            results += values

        return Collection(results)

Response: Collapse the collection items into a single element


Instruction:
Write a Python 3 function that can
determine if an element is in the collection.

Response:
def contains(self, key, value=None):
        """
        Determine if an element is in the collection

        :param key: The element
        :type key: int or str

        :param value: The value of the element
        :type value: mixed

        :return: Whether the element is in the collection
        :rtype: bool
        """
        if value is not None:
            if isinstance(self._items, list):
                return key in self._items and self._items[self._items.index(key)] == value

            return self._items.get(key) == value

        return key in self._items


Instruction:
Write a Python 3 script to
get a list with the values of a given key.

Response:
def lists(self, value, key=None):
        """
        Get a list with the values of a given key

        :rtype: list
        """
        results = map(lambda x: x[value], self._items)

        return list(results)


Instruction:
Can you implement a function in Python 3 that
runs a map over each of the items in the collection.

Response:
def map(self, callback):
        """
        Run a map over each of the item.

        :param callback: The map function
        :type callback: callable

        :rtype: Collection
        """
        if isinstance(self._items, dict):
            return Collection(list(map(callback, self._items.values())))

        return Collection(list(map(callback, self._items)))


Instruction:
Here you have a function in Python 3, explain what it does
def unique(self):
        seen = set()
        seen_add = seen.add

        return Collection([x for x in self._items if not (x in seen or seen_add(x))])

Response: Return only unique items from the collection list.


Instruction:
Create a Python 3 function to
load a set of relationships onto the collection.

Response:
def load(self, *relations):
        """
        Load a set of relationships onto the collection.
        """
        if len(self._items) > 0:
            query = self.first().new_query().with_(*relations)

            self._items = query.eager_load_relations(self._items)

        return self


Instruction:
Can you implement a function in Python 3 that
compiles a foreign key command.

Response:
def compile_foreign(self, blueprint, command, _):
        """
        Compile a foreign key command.

        :param blueprint: The blueprint
        :type blueprint: Blueprint

        :param command: The command
        :type command: Fluent

        :rtype: str
        """
        table = self.wrap_table(blueprint)

        on = self.wrap_table(command.on)

        columns = self.columnize(command.columns)

        on_columns = self.columnize(command.references
                                    if isinstance(command.references, list)
                                    else [command.references])

        sql = 'ALTER TABLE %s ADD CONSTRAINT %s ' % (table, command.index)

        sql += 'FOREIGN KEY (%s) REFERENCES %s (%s)' % (columns, on, on_columns)

        if command.get('on_delete'):
            sql += ' ON DELETE %s' % command.on_delete

        if command.get('on_update'):
            sql += ' ON UPDATE %s' % command.on_update

        return sql


Instruction:
How would you explain what the following Python 3 function does
def _get_columns(self, blueprint):
        columns = []

        for column in blueprint.get_added_columns():
            sql = self.wrap(column) + ' ' + self._get_type(column)

            columns.append(self._add_modifiers(sql, blueprint, column))

        return columns

Response: Get the blueprint s columns definitions.


Instruction:
Here you have a function in Python 3, explain what it does
def _add_modifiers(self, sql, blueprint, column):
        for modifier in self._modifiers:
            method = '_modify_%s' % modifier

            if hasattr(self, method):
                sql += getattr(self, method)(blueprint, column)

        return sql

Response: Add the column modifiers to the sql


Instruction:
Create a Python 3 function for
getting the pivot attributes from a model.

Response:
def _clean_pivot_attributes(self, model):
        """
        Get the pivot attributes from a model.

        :type model: eloquent.Model
        """
        values = {}
        delete_keys = []

        for key, value in model.get_attributes().items():
            if key.find('pivot_') == 0:
                values[key[6:]] = value

                delete_keys.append(key)

        for key in delete_keys:
            delattr(model, key)

        return values


Instruction:
Write a Python 3 function that can
return a QuerySet that will add the constraints for a relationship count query on the same table.

Response:
def get_relation_count_query_for_self_join(self, query, parent):
        """
        Add the constraints for a relationship count query on the same table.

        :type query: eloquent.orm.Builder
        :type parent: eloquent.orm.Builder

        :rtype: eloquent.orm.Builder
        """
        query.select(QueryExpression('COUNT(*)'))

        table_prefix = self._query.get_query().get_connection().get_table_prefix()

        hash_ = self.get_relation_count_hash()
        query.from_('%s AS %s%s' % (self._table, table_prefix, hash_))

        key = self.wrap(self.get_qualified_parent_key_name())

        return query.where('%s.%s' % (hash_, self._foreign_key), '=', QueryExpression(key))


Instruction:
Here you have a function in Python 3, explain what it does
def _get_select_columns(self, columns=None):
        if columns == ['*'] or columns is None:
            columns = ['%s.*' % self._related.get_table()]

        return columns + self._get_aliased_pivot_columns()

Response: Set the select clause for the relation query.


Instruction:
Write a Python 3 script for
getting the pivot columns for the relation.

Response:
def _get_aliased_pivot_columns(self):
        """
        Get the pivot columns for the relation.

        :rtype: list
        """
        defaults = [self._foreign_key, self._other_key]

        columns = []

        for column in defaults + self._pivot_columns:
            value = '%s.%s AS pivot_%s' % (self._table, column, column)
            if value not in columns:
                columns.append('%s.%s AS pivot_%s' % (self._table, column, column))

        return columns


Instruction:
Implement a Python 3 function for
setting the join clause for the relation query.

Response:
def _set_join(self, query=None):
        """
        Set the join clause for the relation query.

        :param query: The query builder
        :type query: eloquent.orm.Builder

        :return: self
        :rtype: BelongsToMany
        """
        if not query:
            query = self._query

        base_table = self._related.get_table()

        key = '%s.%s' % (base_table, self._related.get_key_name())

        query.join(self._table, key, '=', self.get_other_key())

        return self


Instruction:
How would you code a function in Python 3 to
match the eagerly loaded results to their parents.

Response:
def match(self, models, results, relation):
        """
        Match the eagerly loaded results to their parents.

        :type models: list
        :type results: Collection
        :type relation:  str
        """
        dictionary = self._build_dictionary(results)

        for model in models:
            key = model.get_key()

            if key in dictionary:
                collection = self._related.new_collection(dictionary[key])

                model.set_relation(relation, collection)

        return models


Instruction:
Given the following Python 3 function, write the documentation
def save(self, model, joining=None, touch=True):
        if joining is None:
            joining = {}

        model.save({'touch': False})

        self.attach(model.get_key(), joining, touch)

        return model

Response: Save a new model and attach it to the parent model.


Instruction:
Can you tell what is the following Python 3 function doing
def _attach_new(self, records, current, touch=True):
        changes = {
            'attached': [],
            'updated': []
        }

        for id, attributes in records.items():
            if id not in current:
                self.attach(id, attributes, touch)

                changes['attached'].append(id)
            elif len(attributes) > 0 and self.update_existing_pivot(id, attributes, touch):
                changes['updated'].append(id)

        return changes

Response: Attach all of the IDs that aren t in the current dict.


Instruction:
How would you implement a function in Python 3 that
updates an existing pivot record on the table.

Response:
def update_existing_pivot(self, id, attributes, touch=True):
        """
        Update an existing pivot record on the table.
        """
        if self.updated_at() in self._pivot_columns:
            attributes = self.set_timestamps_on_attach(attributes, True)

        updated = self._new_picot_statement_for_id(id).update(attributes)

        if touch:
            self.touch_if_touching()

        return updated


Instruction:
Make a summary of the following Python 3 code
def query(self):
        query = QueryBuilder(self, self._query_grammar, self._post_processor)

        return query

Response: Begin a fluent query


Instruction:
Write a Python 3 function that can
force a soft delete on a soft deleted model.

Response:
def force_delete(self):
        """
        Force a hard delete on a soft deleted model.
        """
        self._force_deleting = True

        self.delete()

        self._force_deleting = False


Instruction:
How would you code a function in Python 3 to
perform the actual delete query on this instance.

Response:
def _do_perform_delete_on_model(self):
        """
        Perform the actual delete query on this model instance.
        """
        if self._force_deleting:
            return self.with_trashed().where(self.get_key_name(), self.get_key()).force_delete()

        return self._run_soft_delete()


Instruction:
Make a summary of the following Python 3 code
def restore(self):
        setattr(self, self.get_deleted_at_column(), None)

        self.set_exists(True)

        result = self.save()

        return result

Response: Restore a soft - deleted model instance.


Instruction:
How would you implement a function in Python 3 that
adds a join clause to the query

Response:
def join(self, table, one=None,
             operator=None, two=None, type='inner', where=False):
        """
        Add a join clause to the query

        :param table: The table to join with, can also be a JoinClause instance
        :type table: str or JoinClause

        :param one: The first column of the join condition
        :type one: str

        :param operator: The operator of the join condition
        :type operator: str

        :param two: The second column of the join condition
        :type two: str

        :param type: The join type
        :type type: str

        :param where: Whether to use a "where" rather than a "on"
        :type where: bool

        :return: The current QueryBuilder instance
        :rtype: QueryBuilder
        """
        if isinstance(table, JoinClause):
            self.joins.append(table)
        else:
            if one is None:
                raise ArgumentError('Missing "one" argument')

            join = JoinClause(table, type)

            self.joins.append(join.on(
                one, operator, two, 'and', where
            ))

        return self


Instruction:
Can you generate a brief explanation for the following Python 3 code
def join_where(self, table, one, operator, two, type='inner'):
        return self.join(table, one, operator, two, type, True)

Response: Add a join where clause to the current QueryBuilder instance


Instruction:
Can you implement a function in Python 3 that
adds a where in with a sub select to the query

Response:
def _where_in_sub(self, column, query, boolean, negate=False):
        """
        Add a where in with a sub select to the query

        :param column: The column
        :type column: str

        :param query: A QueryBuilder instance
        :type query: QueryBuilder

        :param boolean: The boolean operator
        :type boolean: str

        :param negate: Whether it is a not where in
        :param negate: bool

        :return: The current QueryBuilder instance
        :rtype: QueryBuilder
        """
        if negate:
            type = 'not_in_sub'
        else:
            type = 'in_sub'

        self.wheres.append({
            'type': type,
            'column': column,
            'query': query,
            'boolean': boolean
        })

        self.merge_bindings(query)

        return self


Instruction:
Make a summary of the following Python 3 code
def or_having(self, column, operator=None, value=None):
        return self.having(column, operator, value, 'or')

Response: Add a having clause to the current QueryBuilder instance


Instruction:
Can you generate the documentation for the following Python 3 function
def union(self, query, all=False):
        self.unions.append({
            'query': query,
            'all': all
        })

        return self.merge_bindings(query)

Response: Adds a union statement to the query


Instruction:
Write a Python 3 script for
executing a query for a single record by id

Response:
def find(self, id, columns=None):
        """
        Execute a query for a single record by id

        :param id: The id of the record to retrieve
        :type id: mixed

        :param columns: The columns of the record to retrive
        :type columns: list

        :return: mixed
        :rtype: mixed
        """
        if not columns:
            columns = ['*']

        return self.where('id', '=', id).first(1, columns)


Instruction:
Can you generate the documentation for the following Python 3 function
def first(self, limit=1, columns=None):
        if not columns:
            columns = ['*']

        results = self.take(limit).get(columns)

        if len(results) > 0:
            return results[0]

        return

Response: Execute the query and get the first result


Instruction:
Make a summary of the following Python 3 code
def get_fresh(self, columns=None):
        if not columns:
            columns = ['*']

        if not self.columns:
            self.columns = columns

        return self._processor.process_select(self, self._run_select())

Response: Execute the query as a fresh select statement and return the result.


Instruction:
Make a summary of the following Python 3 code
def _get_list_select(self, column, key=None):
        if key is None:
            elements = [column]
        else:
            elements = [column, key]

        select = []
        for elem in elements:
            dot = elem.find('.')

            if dot >= 0:
                select.append(column[dot + 1:])
            else:
                select.append(elem)

        return select

Response: Returns the list of values that should be used in a list of key - value pairs.


Instruction:
Can you generate the documentation for the following Python 3 function
def increment(self, column, amount=1, extras=None):
        wrapped = self._grammar.wrap(column)

        if extras is None:
            extras = {}

        columns = {
            column: self.raw('%s + %s' % (wrapped, amount))
        }
        columns.update(extras)

        return self.update(**columns)

Response: Increment a column s value by a given amount


Instruction:
How would you explain what the following Python 3 function does
def delete(self, id=None):
        if id is not None:
            self.where('id', '=', id)

        sql = self._grammar.compile_delete(self)

        return self._connection.delete(sql, self.get_bindings())

Response: Delete a record from the database.


Instruction:
Implement a Python 3 function for
merging a list of where clauses and bindings into the current where list

Response:
def merge_wheres(self, wheres, bindings):
        """
        Merge a list of where clauses and bindings

        :param wheres: A list of where clauses
        :type wheres: list

        :param bindings: A list of bindings
        :type bindings: list

        :rtype: None
        """
        self.wheres = self.wheres + wheres
        self._bindings['where'] = self._bindings['where'] + bindings


Instruction:
Can you create a Python 3 function that
executes the migrate make command.

Response:
def execute(self, i, o):
        """
        Executes the command.

        :type i: cleo.inputs.input.Input
        :type o: cleo.outputs.output.Output
        """
        super(MigrateMakeCommand, self).execute(i, o)

        creator = MigrationCreator()

        name = i.get_argument('name')
        table = i.get_option('table')
        create = bool(i.get_option('create'))

        if not table and create is not False:
            table = create

        path = i.get_option('path')
        if path is None:
            path = self._get_migration_path()

        file_ = self._write_migration(creator, name, table, create, path)

        o.writeln('<info>Create migration: <comment>%s</comment></info>' % file_)


Instruction:
Make a summary of the following Python 3 code
def remove(self, builder, model):
        column = model.get_qualified_deleted_at_column()

        query = builder.get_query()

        wheres = []
        for where in query.wheres:
            # If the where clause is a soft delete date constraint,
            # we will remove it from the query and reset the keys
            # on the wheres. This allows the developer to include
            # deleted model in a relationship result set that is lazy loaded.
            if not self._is_soft_delete_constraint(where, column):
                wheres.append(where)

        query.wheres = wheres

Response: Removes the scope from a given builder.


Instruction:
Create a Python 3 function to
extend the query builder with the needed functions.

Response:
def extend(self, builder):
        """
        Extend the query builder with the needed functions.

        :param builder: The query builder
        :type builder: eloquent.orm.builder.Builder
        """
        for extension in self._extensions:
            getattr(self, '_add_%s' % extension)(builder)

        builder.on_delete(self._on_delete)


Instruction:
Can you tell what is the following Python 3 function doing
def _only_trashed(self, builder):
        model = builder.get_model()

        self.remove(builder, model)

        builder.get_query().where_not_null(model.get_qualified_deleted_at_column())

Response: The only - trashed extension.


Instruction:
Can you generate the documentation for the following Python 3 function
def _add_fluent_indexes(self):
        for column in self._columns:
            for index in ['primary', 'unique', 'index']:
                column_index = column.get(index)

                if column_index is True:
                    getattr(self, index)(column.name)

                    break
                elif column_index:
                    getattr(self, index)(column.name, column_index)

                    break

Response: Add the index commands fluently specified on columns


Instruction:
Can you generate the documentation for the following Python 3 function
def big_integer(self, column, auto_increment=False, unsigned=False):
        return self._add_column('big_integer', column,
                                auto_increment=auto_increment,
                                unsigned=unsigned)

Response: Create a new big integer column on the table.


Instruction:
Can you generate the documentation for the following Python 3 function
def medium_integer(self, column, auto_increment=False, unsigned=False):
        return self._add_column('medium_integer', column,
                                auto_increment=auto_increment,
                                unsigned=unsigned)

Response: Create a new medium integer column on the table.


Instruction:
Can you create a Python 3 function that
creates a new tiny integer column on the table.

Response:
def tiny_integer(self, column, auto_increment=False, unsigned=False):
        """
        Create a new tiny integer column on the table.

        :param column: The column
        :type column: str

        :type auto_increment: bool

        :type unsigned: bool

        :rtype: Fluent
        """
        return self._add_column('tiny_integer', column,
                                auto_increment=auto_increment,
                                unsigned=unsigned)


Instruction:
Implement a Python 3 function for
adding a new column to the blueprint.

Response:
def _add_column(self, type, name, **parameters):
        """
        Add a new column to the blueprint.

        :param type: The column type
        :type type: str

        :param name: The column name
        :type name: str

        :param parameters: The column parameters
        :type parameters: dict

        :rtype: Fluent
        """
        parameters.update({
            'type': type,
            'name': name
        })

        column = Fluent(**parameters)
        self._columns.append(column)

        return column


Instruction:
Write a Python 3 script to
get the ALTER TABLE SQL statement for the given table diff.

Response:
def get_alter_table_sql(self, diff):
        """
        Get the ALTER TABLE SQL statement

        :param diff: The table diff
        :type diff: eloquent.dbal.table_diff.TableDiff

        :rtype: list
        """
        #sql = self._get_simple_alter_table_sql(diff)

        from_table = diff.from_table
        if not isinstance(from_table, Table):
            raise Exception('SQLite platform requires for the alter table the table diff '
                            'referencing the original table')

        table = from_table.clone()
        columns = {}
        old_column_names = {}
        new_column_names = {}
        column_sql = []
        for column_name, column in table.get_columns().items():
            column_name = column_name.lower()
            columns[column_name] = column
            old_column_names[column_name] = column.get_name()
            new_column_names[column_name] = column.get_name()

        for column_name, column in diff.removed_columns.items():
            column_name = column_name.lower()
            if column_name in columns:
                del columns[column_name]
                del old_column_names[column_name]
                del new_column_names[column_name]

        for old_column_name, column in diff.renamed_columns.items():
            old_column_name = old_column_name.lower()
            if old_column_name in columns:
                del columns[old_column_name]

            columns[column.get_name().lower()] = column

            if old_column_name in new_column_names:
                new_column_names[old_column_name] = column.get_name()

        for old_column_name, column_diff in diff.changed_columns.items():
            if old_column_name in columns:
                del columns[old_column_name]

            columns[column_diff.column.get_name().lower()] = column_diff.column

            if old_column_name in new_column_names:
                new_column_names[old_column_name] = column_diff.column.get_name()

        for column_name, column in diff.added_columns.items():
            columns[column_name.lower()] = column

        sql = []
        table_sql = []

        data_table = Table('__temp__' + table.get_name())

        new_table = Table(table.get_name(), columns,
                          self.get_primary_index_in_altered_table(diff),
                          self.get_foreign_keys_in_altered_table(diff))
        new_table.add_option('alter', True)

        sql = self.get_pre_alter_table_index_foreign_key_sql(diff)
        sql.append('CREATE TEMPORARY TABLE %s AS SELECT %s FROM %s'
                   % (data_table.get_name(), ', '.join(old_column_names.values()), table.get_name()))
        sql.append(self.get_drop_table_sql(from_table))

        sql += self.get_create_table_sql(new_table)
        sql.append('INSERT INTO %s (%s) SELECT %s FROM %s'
                   % (new_table.get_name(),
                      ', '.join(new_column_names.values()),
                      ', '.join(old_column_names.values()),
                      data_table.get_name()))
        sql.append(self.get_drop_table_sql(data_table))

        sql += self.get_post_alter_table_index_foreign_key_sql(diff)

        return sql


Instruction:
Make a summary of the following Python 3 code
def get_foreign_keys_in_altered_table(self, diff):
        foreign_keys = diff.from_table.get_foreign_keys()
        column_names = self.get_column_names_in_altered_table(diff)

        for key, constraint in foreign_keys.items():
            changed = False
            local_columns = []
            for column_name in constraint.get_local_columns():
                normalized_column_name = column_name.lower()
                if normalized_column_name not in column_names:
                    del foreign_keys[key]
                    break
                else:
                    local_columns.append(column_names[normalized_column_name])
                    if column_name != column_names[normalized_column_name]:
                        changed = True

            if changed:
                pass

        return foreign_keys

Response: Returns a list of foreign keys that are not in the table that have been modified.


Instruction:
How would you implement a function in Python 3 that
adds the constraints for a relationship count query.

Response:
def get_relation_count_query(self, query, parent):
        """
        Add the constraints for a relationship count query.

        :type query: eloquent.orm.Builder
        :type parent: eloquent.orm.Builder

        :rtype: Builder
        """
        query.select(QueryExpression('COUNT(*)'))

        other_key = self.wrap('%s.%s' % (query.get_model().get_table(), self._other_key))

        return query.where(self.get_qualified_foreign_key(), '=', QueryExpression(other_key))


Instruction:
Implement a function in Python 3 to
match the eagerly loaded results to their parents.

Response:
def match(self, models, results, relation):
        """
        Match the eagerly loaded results to their parents.

        :type models: list
        :type results: Collection
        :type relation:  str
        """
        foreign = self._foreign_key

        other = self._other_key

        dictionary = {}

        for result in results:
            dictionary[result.get_attribute(other)] = result

        for model in models:
            value = model.get_attribute(foreign)

            if value in dictionary:
                model.set_relation(relation, dictionary[value])

        return models


Instruction:
How would you explain what the following Python 3 function does
def associate(self, model):
        self._parent.set_attribute(self._foreign_key, model.get_attribute(self._other_key))

        return self._parent.set_relation(self._relation, model)

Response: Associate the given model instance with the given parent.


Instruction:
Given the following Python 3 function, write the documentation
def dissociate(self):
        self._parent.set_attribute(self._foreign_key, None)

        return self._parent.set_relation(self._relation, None)

Response: Dissociate previously associated model from the given parent.


Instruction:
Implement a Python 3 function for
executing the command. :type i: cleo.inputs.input.Input :type o: cleo.outputs.output.Output

Response:
def execute(self, i, o):
        """
        Executes the command.

        :type i: cleo.inputs.input.Input
        :type o: cleo.outputs.output.Output
        """
        super(StatusCommand, self).execute(i, o)

        database = i.get_option('database')
        repository = DatabaseMigrationRepository(self._resolver, 'migrations')

        migrator = Migrator(repository, self._resolver)

        if not migrator.repository_exists():
            return o.writeln('<error>No migrations found</error>')

        self._prepare_database(migrator, database, i, o)

        path = i.get_option('path')

        if path is None:
            path = self._get_migration_path()

        ran = migrator.get_repository().get_ran()

        migrations = []
        for migration in migrator._get_migration_files(path):
            if migration in ran:
                migrations.append(['<fg=cyan>%s</>' % migration, '<info>Yes</info>'])
            else:
                migrations.append(['<fg=cyan>%s</>' % migration, '<fg=red>No</>'])

        if migrations:
            table = self.get_helper('table')
            table.set_headers(['Migration', 'Ran?'])
            table.set_rows(migrations)
            table.render(o)
        else:
            return o.writeln('<error>No migrations found</error>')

        for note in migrator.get_notes():
            o.writeln(note)


Instruction:
Can you tell what is the following Python 3 function doing
def compile_rename_column(self, blueprint, command, connection):
        # The code is a little complex. It will propably change
        # if we support complete diffs in dbal
        sql = []

        schema = connection.get_schema_manager()
        table = self.get_table_prefix() + blueprint.get_table()

        column = connection.get_column(table, command.from_)

        columns = schema.list_table_columns(table)
        indexes = schema.list_table_indexes(table)
        foreign_keys = schema.list_table_foreign_keys(table)

        diff = self._get_renamed_diff(blueprint, command, column, schema)
        renamed_columns = diff.renamed_columns

        old_column_names = list(map(lambda x: x.get_name(), columns))

        # We build the new column names
        new_column_names = []
        for column_name in old_column_names:
            if column_name in renamed_columns:
                new_column_names.append(renamed_columns[column_name].get_name())
            else:
                new_column_names.append(column_name)

        # We create a temporary table and insert the data into it
        temp_table = '__temp__' + self.get_table_prefix() + blueprint.get_table()
        sql.append('CREATE TEMPORARY TABLE %s AS SELECT %s FROM %s'
                   % (temp_table, self.columnize(old_column_names), table))

        # We drop the current table
        sql += Blueprint(table).drop().to_sql(None, self)

        # Building the list a new columns
        new_columns = []
        for column in columns:
            for column_name, changed_column in renamed_columns.items():
                if column_name == column.get_name():
                    new_columns.append(changed_column)

        # Here we will try to rebuild a new blueprint to create a new table
        # with the original name
        new_blueprint = Blueprint(table)
        new_blueprint.create()
        primary = []
        for column in columns:
            # Mapping the database type to the blueprint type
            type = schema.get_database_platform().TYPE_MAPPING[column.get_type().lower()]

            # If the column is a primary, we will add it to the blueprint later
            if column.get_platform_option('pk'):
                primary.append(column.get_name())

            # If the column is not one that's been renamed we reinsert it into the blueprint
            if column.get_name() not in renamed_columns.keys():
                col = getattr(new_blueprint, type)(column.get_name())

                # If the column is nullable, we flag it
                if not column.get_notnull():
                    col.nullable()

                # If the column has a default value, we add it
                if column.get_default() is not None:
                    col.default(QueryExpression(column.get_default()))

        # Inserting the renamed columns into the blueprint
        for column in new_columns:
            type = schema.get_database_platform().TYPE_MAPPING[column.get_type().lower()]

            col = getattr(new_blueprint, type)(column.get_name())
            if not column.get_notnull():
                col.nullable()

            if column.get_default() is not None:
                col.default(QueryExpression(column.get_default()))

        # We add the primary keys
        if primary:
            new_blueprint.primary(primary)

        # We rebuild the indexes
        for index in indexes:
            index_columns = index['columns']
            new_index_columns = []
            index_name = index['name']

            for column_name in index_columns:
                if column_name in renamed_columns:
                    new_index_columns.append(renamed_columns[column_name].get_name())
                else:
                    new_index_columns.append(column_name)

            if index_columns != new_index_columns:
                index_name = None

            if index['unique']:
                new_blueprint.unique(new_index_columns, index_name)
            else:
                new_blueprint.index(index['columns'], index_name)

        for foreign_key in foreign_keys:
            fkey_from = foreign_key['from']
            if fkey_from in renamed_columns:
                fkey_from = renamed_columns[fkey_from].get_name()

            new_blueprint.foreign(fkey_from)\
                .references(foreign_key['to'])\
                .on(foreign_key['table'])\
                .on_delete(foreign_key['on_delete'])\
                .on_update(foreign_key['on_update'])

        # We create the table
        sql += new_blueprint.to_sql(None, self)

        # We reinsert the data into the new table
        sql.append('INSERT INTO %s (%s) SELECT %s FROM %s'
                   % (self.wrap_table(table),
                      ', '.join(new_column_names),
                      self.columnize(old_column_names),
                      self.wrap_table(temp_table)
                      ))

        # Finally we drop the temporary table
        sql += Blueprint(temp_table).drop().to_sql(None, self)

        return sql

Response: Compile a rename column command.


Instruction:
Can you create a Python 3 function that
compiles a change column command into a series of SQL statements.

Response:
def compile_change(self, blueprint, command, connection):
        """
        Compile a change column command into a series of SQL statement.

        :param blueprint: The blueprint
        :type blueprint: eloquent.schema.Blueprint

        :param command: The command
        :type command: Fluent

        :param connection: The connection
        :type connection: eloquent.connections.Connection

        :rtype: list
        """
        sql = []

        schema = connection.get_schema_manager()
        table = self.get_table_prefix() + blueprint.get_table()

        columns = schema.list_table_columns(table)
        indexes = schema.list_table_indexes(table)
        foreign_keys = schema.list_table_foreign_keys(table)

        diff = self._get_changed_diff(blueprint, schema)
        blueprint_changed_columns = blueprint.get_changed_columns()
        changed_columns = diff.changed_columns

        temp_table = '__temp__' + self.get_table_prefix() + blueprint.get_table()
        sql.append('CREATE TEMPORARY TABLE %s AS SELECT %s FROM %s'
                   % (temp_table, self.columnize(list(map(lambda x: x.get_name(), columns))), table))
        sql += Blueprint(table).drop().to_sql(None, self)

        new_columns = []
        for column in columns:
            for column_name, changed_column in changed_columns.items():
                if column_name == column.get_name():
                    for blueprint_column in blueprint_changed_columns:
                        if blueprint_column.name == column_name:
                            new_columns.append(blueprint_column)
                            break

                    break

        new_blueprint = Blueprint(table)
        new_blueprint.create()
        primary = []
        new_column_names = []
        for column in columns:
            type = schema.get_database_platform().TYPE_MAPPING[column.get_type().lower()]

            if column.get_platform_option('pk'):
                primary.append(column.get_name())

            if column.get_name() not in changed_columns:
                col = getattr(new_blueprint, type)(column.get_name())
                if not column.get_notnull():
                    col.nullable()

                new_column_names.append(column.get_name())

        for column in new_columns:
            column.change = False
            new_blueprint._add_column(**column.get_attributes())
            new_column_names.append(column.name)

        if primary:
            new_blueprint.primary(primary)

        for index in indexes:
            if index['unique']:
                new_blueprint.unique(index['columns'], index['name'])
            else:
                new_blueprint.index(index['columns'], index['name'])

        for foreign_key in foreign_keys:
            new_blueprint.foreign(foreign_key['from'])\
                .references(foreign_key['to'])\
                .on(foreign_key['table'])\
                .on_delete(foreign_key['on_delete'])\
                .on_update(foreign_key['on_update'])

        sql += new_blueprint.to_sql(None, self)
        sql.append('INSERT INTO %s (%s) SELECT %s FROM %s'
                   % (self.wrap_table(table),
                      ', '.join(sorted(new_column_names)),
                      self.columnize(sorted(list(map(lambda x: x.get_name(), columns)))),
                      self.wrap_table(temp_table)
                      ))
        sql += Blueprint(temp_table).drop().to_sql(None, self)

        return sql


Instruction:
How would you code a function in Python 3 to
compile a create table command.

Response:
def compile_create(self, blueprint, command, _):
        """
        Compile a create table command.
        """
        columns = ', '.join(self._get_columns(blueprint))

        sql = 'CREATE TABLE %s (%s' % (self.wrap_table(blueprint), columns)

        sql += self._add_foreign_keys(blueprint)

        sql += self._add_primary_keys(blueprint)

        return sql + ')'


Instruction:
Create a Python 3 function for
matching the eargerly loaded resuls to their single parents.

Response:
def match_one(self, models, results, relation):
        """
        Match the eargerly loaded resuls to their single parents.

        :param models: The parents
        :type models: list

        :param results: The results collection
        :type results: Collection

        :param relation: The relation
        :type relation: str

        :rtype: list
        """
        return self._match_one_or_many(models, results, relation, 'one')


Instruction:
Can you tell what is the following Python 3 function doing
def match_many(self, models, results, relation):
        return self._match_one_or_many(models, results, relation, 'many')

Response: Match the eargerly loaded resuls to their single parents.


Instruction:
Write a Python 3 script to
match the eargerly loaded resuls to their single parents.

Response:
def _match_one_or_many(self, models, results, relation, type):
        """
        Match the eargerly loaded resuls to their single parents.

        :param models: The parents
        :type models: list

        :param results: The results collection
        :type results: Collection

        :param relation: The relation
        :type relation: str

        :param type: The match type
        :type type: str

        :rtype: list
        """
        dictionary = self._build_dictionary(results)

        for model in models:
            key = model.get_attribute(self._local_key)

            if key in dictionary:
                value = self._get_relation_value(dictionary, key, type)

                model.set_relation(relation, value)

        return models


Instruction:
Implement a Python 3 function for
getting the value of the relationship by one or many type.

Response:
def _get_relation_value(self, dictionary, key, type):
        """
        Get the value of the relationship by one or many type.

        :type dictionary: dict
        :type key: str
        :type type: str
        """
        value = dictionary[key]

        if type == 'one':
            return value[0]

        return self._related.new_collection(value)


Instruction:
Write a Python 3 script to
get the first related model record matching the attributes or instantiate it.

Response:
def first_or_new(self, _attributes=None, **attributes):
        """
        Get the first related model record matching the attributes or instantiate it.

        :param attributes:  The attributes
        :type attributes: dict

        :rtype: Model
        """
        if _attributes is not None:
            attributes.update(_attributes)

        instance = self.where(attributes).first()

        if instance is None:
            instance = self._related.new_instance()
            instance.set_attribute(self.get_plain_foreign_key(), self.get_parent_key())

        return instance


Instruction:
Create a Python 3 function for
getting the first related record matching the attributes or create it.

Response:
def first_or_create(self, _attributes=None, **attributes):
        """
        Get the first related record matching the attributes or create it.

        :param attributes:  The attributes
        :type attributes: dict

        :rtype: Model
        """
        if _attributes is not None:
            attributes.update(_attributes)

        instance = self.where(attributes).first()

        if instance is None:
            instance = self.create(**attributes)

        return instance