Instruction:
Given the following Python 3 function, write the documentation
def glyphs2ufo(options):
    if options.output_dir is None:
        options.output_dir = os.path.dirname(options.glyphs_file) or "."

    if options.designspace_path is None:
        options.designspace_path = os.path.join(
            options.output_dir,
            os.path.basename(os.path.splitext(options.glyphs_file)[0]) + ".designspace",
        )

    # If options.instance_dir is None, instance UFO paths in the designspace
    # file will either use the value in customParameter's FULL_FILENAME_KEY or be
    # made relative to "instance_ufos/".
    glyphsLib.build_masters(
        options.glyphs_file,
        options.output_dir,
        options.instance_dir,
        designspace_path=options.designspace_path,
        minimize_glyphs_diffs=options.no_preserve_glyphsapp_metadata,
        propagate_anchors=options.propagate_anchors,
        normalize_ufos=options.normalize_ufos,
        create_background_layers=options.create_background_layers,
        generate_GDEF=options.generate_GDEF,
        store_editor_state=not options.no_store_editor_state,
    )

Response: Converts a Glyphs. app source file into UFO masters and a designspace file.


Instruction:
Make a summary of the following Python 3 code
def ufo2glyphs(options):
    import fontTools.designspaceLib
    import defcon

    sources = options.designspace_file_or_UFOs
    designspace_file = None
    if (
        len(sources) == 1
        and sources[0].endswith(".designspace")
        and os.path.isfile(sources[0])
    ):
        designspace_file = sources[0]
        designspace = fontTools.designspaceLib.DesignSpaceDocument()
        designspace.read(designspace_file)
        object_to_read = designspace
    elif all(source.endswith(".ufo") and os.path.isdir(source) for source in sources):
        ufos = [defcon.Font(source) for source in sources]
        ufos.sort(
            key=lambda ufo: [  # Order the masters by weight and width
                ufo.info.openTypeOS2WeightClass or 400,
                ufo.info.openTypeOS2WidthClass or 5,
            ]
        )
        object_to_read = ufos
    else:
        print(
            "Please specify just one designspace file *or* one or more "
            "UFOs. They must end in '.designspace' or '.ufo', respectively.",
            file=sys.stderr,
        )
        return 1

    font = glyphsLib.to_glyphs(
        object_to_read, minimize_ufo_diffs=options.no_preserve_glyphsapp_metadata
    )

    # Make the Glyphs file more suitable for roundtrip:
    font.customParameters["Disable Last Change"] = options.enable_last_change
    font.disablesAutomaticAlignment = options.enable_automatic_alignment

    if options.output_path:
        font.save(options.output_path)
    else:
        if designspace_file:
            filename_to_write = os.path.splitext(designspace_file)[0] + ".glyphs"
        else:
            filename_to_write = os.path.join(
                os.path.dirname(sources[0]),
                font.familyName.replace(" ", "") + ".glyphs",
            )
        font.save(filename_to_write)

Response: Convert one designspace file or one or more UFOs to Glyphs. app source file.


Instruction:
Here you have a function in Python 3, explain what it does
def _has_manual_kern_feature(font):
    return any(f for f in font.features if f.name == "kern" and not f.automatic)

Response: Return true if the GSFont contains a manually written kern feature.


Instruction:
Can you generate the documentation for the following Python 3 function
def to_ufo_family_user_data(self, ufo):
    if not self.use_designspace:
        ufo.lib[FONT_USER_DATA_KEY] = dict(self.font.userData)

Response: Set family - wide user data as Glyphs does.


Instruction:
Create a Python 3 function for
setting master - specific user data as Glyphs does.

Response:
def to_ufo_master_user_data(self, ufo, master):
    """Set master-specific user data as Glyphs does."""
    for key in master.userData.keys():
        if _user_data_has_no_special_meaning(key):
            ufo.lib[key] = master.userData[key]

    # Restore UFO data files. This code assumes that all paths are POSIX paths.
    if UFO_DATA_KEY in master.userData:
        for filename, data in master.userData[UFO_DATA_KEY].items():
            ufo.data[filename] = bytes(data)


Instruction:
Write a Python 3 script for
setting the GSFont userData from the designspace family - wide lib data.

Response:
def to_glyphs_family_user_data_from_designspace(self):
    """Set the GSFont userData from the designspace family-wide lib data."""
    target_user_data = self.font.userData
    for key, value in self.designspace.lib.items():
        if key == UFO2FT_FEATURE_WRITERS_KEY and value == DEFAULT_FEATURE_WRITERS:
            # if the designspace contains featureWriters settings that are the
            # same as glyphsLib default settings, there's no need to store them
            continue
        if _user_data_has_no_special_meaning(key):
            target_user_data[key] = value


Instruction:
Write a Python 3 script for
setting the GSFont userData from the UFO family - wide lib data.

Response:
def to_glyphs_family_user_data_from_ufo(self, ufo):
    """Set the GSFont userData from the UFO family-wide lib data."""
    target_user_data = self.font.userData
    try:
        for key, value in ufo.lib[FONT_USER_DATA_KEY].items():
            # Existing values taken from the designspace lib take precedence
            if key not in target_user_data.keys():
                target_user_data[key] = value
    except KeyError:
        # No FONT_USER_DATA in ufo.lib
        pass


Instruction:
Can you generate a brief explanation for the following Python 3 code
def to_glyphs_master_user_data(self, ufo, master):
    target_user_data = master.userData
    for key, value in ufo.lib.items():
        if _user_data_has_no_special_meaning(key):
            target_user_data[key] = value

    # Save UFO data files
    if ufo.data.fileNames:
        from glyphsLib.types import BinaryData

        ufo_data = {}
        for os_filename in ufo.data.fileNames:
            filename = posixpath.join(*os_filename.split(os.path.sep))
            ufo_data[filename] = BinaryData(ufo.data[os_filename])
        master.userData[UFO_DATA_KEY] = ufo_data

Response: Set the GSFontMaster userData from the UFO master - specific lib data.


Instruction:
Given the following Python 3 function, write the documentation
def to_ufos(
    font,
    include_instances=False,
    family_name=None,
    propagate_anchors=True,
    ufo_module=defcon,
    minimize_glyphs_diffs=False,
    generate_GDEF=True,
    store_editor_state=True,
):
    """Take a GSFont object and convert it into one UFO per master.

    Takes in data as Glyphs.app-compatible classes, as documented at
    https://docu.glyphsapp.com/

    If include_instances is True, also returns the parsed instance data.

    If family_name is provided, the master UFOs will be given this name and
    only instances with this name will be returned.

    If generate_GDEF is True, write a `table GDEF {...}` statement in the
    UFO's features.fea, containing GlyphClassDef and LigatureCaretByPos.
    """
    builder = UFOBuilder(
        font,
        ufo_module=ufo_module,
        family_name=family_name,
        propagate_anchors=propagate_anchors,
        minimize_glyphs_diffs=minimize_glyphs_diffs,
        generate_GDEF=generate_GDEF,
        store_editor_state=store_editor_state,
    )

    result = list(builder.masters)

    if include_instances:
        return result, builder.instance_data
    return result

Response: Take a GSFont object and convert it into one UFO per master.


Instruction:
How would you code a function in Python 3 to
take a GSFont object and convert it into a Designspace Document + UFOs.

Response:
def to_designspace(
    font,
    family_name=None,
    instance_dir=None,
    propagate_anchors=True,
    ufo_module=defcon,
    minimize_glyphs_diffs=False,
    generate_GDEF=True,
    store_editor_state=True,
):
    """Take a GSFont object and convert it into a Designspace Document + UFOS.
    The UFOs are available as the attribute `font` of each SourceDescriptor of
    the DesignspaceDocument:

        ufos = [source.font for source in designspace.sources]

    The designspace and the UFOs are not written anywhere by default, they
    are all in-memory. If you want to write them to the disk, consider using
    the `filename` attribute of the DesignspaceDocument and of its
    SourceDescriptor as possible file names.

    Takes in data as Glyphs.app-compatible classes, as documented at
    https://docu.glyphsapp.com/

    If include_instances is True, also returns the parsed instance data.

    If family_name is provided, the master UFOs will be given this name and
    only instances with this name will be returned.

    If generate_GDEF is True, write a `table GDEF {...}` statement in the
    UFO's features.fea, containing GlyphClassDef and LigatureCaretByPos.
    """
    builder = UFOBuilder(
        font,
        ufo_module=ufo_module,
        family_name=family_name,
        instance_dir=instance_dir,
        propagate_anchors=propagate_anchors,
        use_designspace=True,
        minimize_glyphs_diffs=minimize_glyphs_diffs,
        generate_GDEF=generate_GDEF,
        store_editor_state=store_editor_state,
    )
    return builder.designspace


Instruction:
Create a Python 3 function to
convert a UFOs or DesignspaceDocument into a GSFont object.

Response:
def to_glyphs(ufos_or_designspace, glyphs_module=classes, minimize_ufo_diffs=False):
    """
    Take a list of UFOs or a single DesignspaceDocument with attached UFOs
    and converts it into a GSFont object.

    The GSFont object is in-memory, it's up to the user to write it to the disk
    if needed.

    This should be the inverse function of `to_ufos` and `to_designspace`,
    so we should have to_glyphs(to_ufos(font)) == font
    and also to_glyphs(to_designspace(font)) == font
    """
    if hasattr(ufos_or_designspace, "sources"):
        builder = GlyphsBuilder(
            designspace=ufos_or_designspace,
            glyphs_module=glyphs_module,
            minimize_ufo_diffs=minimize_ufo_diffs,
        )
    else:
        builder = GlyphsBuilder(
            ufos=ufos_or_designspace,
            glyphs_module=glyphs_module,
            minimize_ufo_diffs=minimize_ufo_diffs,
        )
    return builder.font


Instruction:
Can you generate the documentation for the following Python 3 function
def path_helper(self, path, view, **kwargs):
        super(FlaskRestyPlugin, self).path_helper(
            path=path,
            view=view,
            **kwargs
        )

        resource = self.get_state().views[view]
        rule = self._rules[resource.rule]

        operations = defaultdict(Operation)
        view_instance = view()
        view_instance.spec_declaration(view, operations, self)

        # add path arguments
        parameters = []
        for arg in rule.arguments:
            parameters.append({
                'name': arg,
                'in': 'path',
                'required': True,
                'type': 'string',
            })
        if parameters:
            operations['parameters'] = parameters

        path.path = FlaskPlugin.flaskpath2openapi(resource.rule)
        path.operations = dict(**operations)

Response: Path helper for Flask - RESTy views.


Instruction:
How would you code a function in Python 3 to
convert UFO class to value.

Response:
def class_to_value(axis, ufo_class):
    """
    >>> class_to_value('wdth', 7)
    125
    """
    if axis == "wght":
        # 600.0 => 600, 250 => 250
        return int(ufo_class)
    elif axis == "wdth":
        return WIDTH_CLASS_TO_VALUE[int(ufo_class)]

    raise NotImplementedError


Instruction:
Can you generate the documentation for the following Python 3 function
def user_loc_string_to_value(axis_tag, user_loc):
    if axis_tag == "wght":
        try:
            value = _nospace_lookup(WEIGHT_CODES, user_loc)
        except KeyError:
            return None
        return class_to_value("wght", value)
    elif axis_tag == "wdth":
        try:
            value = _nospace_lookup(WIDTH_CODES, user_loc)
        except KeyError:
            return None
        return class_to_value("wdth", value)

    # Currently this function should only be called with a width or weight
    raise NotImplementedError

Response: Go from Glyphs UI strings to user space location.


Instruction:
Implement a Python 3 function for
returning the OS class that is closest to the provided user location.

Response:
def user_loc_value_to_class(axis_tag, user_loc):
    """Return the OS/2 weight or width class that is closest to the provided
    user location. For weight the user location is between 0 and 1000 and for
    width it is a percentage.

    >>> user_loc_value_to_class('wght', 310)
    310
    >>> user_loc_value_to_class('wdth', 62)
    2
    """
    if axis_tag == "wght":
        return int(user_loc)
    elif axis_tag == "wdth":
        return min(
            sorted(WIDTH_CLASS_TO_VALUE.items()),
            key=lambda item: abs(item[1] - user_loc),
        )[0]

    raise NotImplementedError


Instruction:
Can you generate a brief explanation for the following Python 3 code
def user_loc_value_to_instance_string(axis_tag, user_loc):
    codes = {}
    if axis_tag == "wght":
        codes = WEIGHT_CODES
    elif axis_tag == "wdth":
        codes = WIDTH_CODES
    else:
        raise NotImplementedError
    class_ = user_loc_value_to_class(axis_tag, user_loc)
    return min(
        sorted((code, class_) for code, class_ in codes.items() if code is not None),
        key=lambda item: abs(item[1] - class_),
    )[0]

Response:
Return the Glyphs UI string that is
    closest to the provided user location.


Instruction:
Write a Python 3 script for
finding the regular master among the GSFontMasters.

Response:
def get_regular_master(font):
    """Find the "regular" master among the GSFontMasters.

    Tries to find the master with the passed 'regularName'.
    If there is no such master or if regularName is None,
    tries to find a base style shared between all masters
    (defaulting to "Regular"), and then tries to find a master
    with that style name. If there is no master with that name,
    returns the first master in the list.
    """
    if not font.masters:
        return None
    regular_name = font.customParameters["Variation Font Origin"]
    if regular_name is not None:
        for master in font.masters:
            if master.name == regular_name:
                return master
    base_style = find_base_style(font.masters)
    if not base_style:
        base_style = "Regular"
    for master in font.masters:
        if master.name == base_style:
            return master
    # Second try: maybe the base style has regular in it as well
    for master in font.masters:
        name_without_regular = " ".join(
            n for n in master.name.split(" ") if n != "Regular"
        )
        if name_without_regular == base_style:
            return master
    return font.masters[0]


Instruction:
Make a summary of the following Python 3 code
def find_base_style(masters):
    if not masters:
        return ""
    base_style = (masters[0].name or "").split()
    for master in masters:
        style = master.name.split()
        base_style = [s for s in style if s in base_style]
    base_style = " ".join(base_style)
    return base_style

Response: Find a base style shared between all masters.


Instruction:
How would you explain what the following Python 3 function does
def interp(mapping, x):
    mapping = sorted(mapping)
    if len(mapping) == 1:
        xa, ya = mapping[0]
        if xa == x:
            return ya
        return x
    for (xa, ya), (xb, yb) in zip(mapping[:-1], mapping[1:]):
        if xa <= x <= xb:
            return ya + float(x - xa) / (xb - xa) * (yb - ya)
    return x

Response: Compute the piecewise linear interpolation given by mapping for input x.


Instruction:
Make a summary of the following Python 3 code
def get_user_loc(self, master_or_instance):
        user_loc = self.default_user_loc

        if self.tag != "wght":
            # The user location is by default the same as the design location.
            user_loc = self.get_design_loc(master_or_instance)

        # Try to guess the user location by looking at the OS/2 weightClass
        # and widthClass. If a weightClass is found, it translates directly
        # to a user location in 0..1000. If a widthClass is found, it
        # translate to a percentage of extension according to the spec, see
        # the mapping named `WIDTH_CLASS_TO_VALUE` at the top.
        if self.user_loc_key is not None and hasattr(
            master_or_instance, self.user_loc_key
        ):
            # Instances have special ways to specify a user location.
            # Only weight and with have a custom user location via a key.
            # The `user_loc_key` gives a "location code" = Glyphs UI string
            user_loc_str = getattr(master_or_instance, self.user_loc_key)
            new_user_loc = user_loc_string_to_value(self.tag, user_loc_str)
            if new_user_loc is not None:
                user_loc = new_user_loc

        # The custom param takes over the key if it exists
        # e.g. for weight:
        #       key = "weight" -> "Bold" -> 700
        # but param = "weightClass" -> 600       => 600 wins
        if self.user_loc_param is not None:
            class_ = master_or_instance.customParameters[self.user_loc_param]
            if class_ is not None:
                user_loc = class_to_value(self.tag, class_)

        # Masters have a customParameter that specifies a user location
        # along custom axes. If this is present it takes precedence over
        # everything else.
        loc_param = master_or_instance.customParameters["Axis Location"]
        try:
            for location in loc_param:
                if location.get("Axis") == self.name:
                    user_loc = int(location["Location"])
        except (TypeError, KeyError):
            pass

        return user_loc

Response: Get the user location of a Glyphs master or instance.


Instruction:
Write a Python 3 function that can
set the user location of a Glyphs master or instance.

Response:
def set_user_loc(self, master_or_instance, value):
        """Set the user location of a Glyphs master or instance."""
        if hasattr(master_or_instance, "instanceInterpolations"):
            # The following code is only valid for instances.
            # Masters also the keys `weight` and `width` but they should not be
            # used, they are deprecated and should only be used to store
            # (parts of) the master's name, but not its location.

            # Try to set the key if possible, i.e. if there is a key, and
            # if there exists a code that can represent the given value, e.g.
            # for "weight": 600 can be represented by SemiBold so we use that,
            # but for 550 there is no code so we will have to set the custom
            # parameter as well.
            if self.user_loc_key is not None and hasattr(
                master_or_instance, self.user_loc_key
            ):
                code = user_loc_value_to_instance_string(self.tag, value)
                value_for_code = user_loc_string_to_value(self.tag, code)
                setattr(master_or_instance, self.user_loc_key, code)
                if self.user_loc_param is not None and value != value_for_code:
                    try:
                        class_ = user_loc_value_to_class(self.tag, value)
                        master_or_instance.customParameters[
                            self.user_loc_param
                        ] = class_
                    except NotImplementedError:
                        # user_loc_value_to_class only works for weight & width
                        pass
            return

        # For masters, set directly the custom parameter (old way)
        # and also the Axis Location (new way).
        # Only masters can have an 'Axis Location' parameter.
        if self.user_loc_param is not None:
            try:
                class_ = user_loc_value_to_class(self.tag, value)
                master_or_instance.customParameters[self.user_loc_param] = class_
            except NotImplementedError:
                pass

        loc_param = master_or_instance.customParameters["Axis Location"]
        if loc_param is None:
            loc_param = []
            master_or_instance.customParameters["Axis Location"] = loc_param
        location = None
        for loc in loc_param:
            if loc.get("Axis") == self.name:
                location = loc
        if location is None:
            loc_param.append({"Axis": self.name, "Location": value})
        else:
            location["Location"] = value


Instruction:
Here you have a function in Python 3, explain what it does
def add_parameter(self, location='query', **kwargs):
        kwargs.setdefault('in', location)
        if kwargs['in'] != 'body':
            kwargs.setdefault('type', 'string')
        self['parameters'].append(kwargs)

Response: Adds a new parameter to the request


Instruction:
Create a Python 3 function to
add a property to the response identified by code

Response:
def add_property_to_response(self, code='200', prop_name='data', **kwargs):
        """Add a property (http://json-schema.org/latest/json-schema-validation.html#anchor64)  # noqa: E501
        to the schema of the response identified by the code"""
        self['responses'] \
            .setdefault(str(code), self._new_operation()) \
            .setdefault('schema', {'type': 'object'}) \
            .setdefault('properties', {}) \
            .setdefault(prop_name, {}) \
            .update(**kwargs)


Instruction:
Write a Python 3 function that can
declare a response for the specified code

Response:
def declare_response(self, code='200', **kwargs):
        """Declare a response for the specified code
        https://github.com/swagger-api/swagger-spec/blob/master/versions/2.0.md#responseObject"""  # noqa: E501
        self['responses'][str(code)] = self._new_operation(**kwargs)


Instruction:
Make a summary of the following Python 3 code
def _deserialize(self, value, attr, data):
        value = super(SanitizedHTML, self)._deserialize(value, attr, data)
        return bleach.clean(
            value,
            tags=self.tags,
            attributes=self.attrs,
            strip=True,
        ).strip()

Response: Deserialize string by sanitizing HTML.


Instruction:
Can you tell what is the following Python 3 function doing
def build_default_endpoint_prefixes(records_rest_endpoints):
    pid_types = set()
    guessed = set()
    endpoint_prefixes = {}

    for key, endpoint in records_rest_endpoints.items():
        pid_type = endpoint['pid_type']
        pid_types.add(pid_type)
        is_guessed = key == pid_type
        is_default = endpoint.get('default_endpoint_prefix', False)

        if is_default:
            if pid_type in endpoint_prefixes and pid_type not in guessed:
                raise ValueError('More than one "{0}" defined.'.format(
                    pid_type
                ))
            endpoint_prefixes[pid_type] = key
            guessed -= {pid_type}
        elif is_guessed and pid_type not in endpoint_prefixes:
            endpoint_prefixes[pid_type] = key
            guessed |= {pid_type}

    not_found = pid_types - set(endpoint_prefixes.keys())
    if not_found:
        raise ValueError('No endpoint-prefix for {0}.'.format(
            ', '.join(not_found)
        ))

    return endpoint_prefixes

Response: Build the default_endpoint_prefixes map.


Instruction:
Make a summary of the following Python 3 code
def obj_or_import_string(value, default=None):
    if isinstance(value, six.string_types):
        return import_string(value)
    elif value:
        return value
    return default

Response: Import string or return object.


Instruction:
Explain what the following Python 3 code does
def load_or_import_from_config(key, app=None, default=None):
    app = app or current_app
    imp = app.config.get(key)
    return obj_or_import_string(imp, default=default)

Response:
Load or import value from config.
   .


Instruction:
Can you generate the documentation for the following Python 3 function
def check_elasticsearch(record, *args, **kwargs):
    def can(self):
        """Try to search for given record."""
        search = request._methodview.search_class()
        search = search.get_record(str(record.id))
        return search.count() == 1

    return type('CheckES', (), {'can': can})()

Response: Returns a method that checks if the record exists in ES index.


Instruction:
How would you implement a function in Python 3 that
resolves PID from a value and return a tuple with the PID and the record resolved.

Response:
def data(self):
        """Resolve PID from a value and return a tuple with PID and the record.

        :returns: A tuple with the PID and the record resolved.
        """
        try:
            return self.resolver.resolve(self.value)
        except PIDDoesNotExistError as pid_error:
            raise PIDDoesNotExistRESTError(pid_error=pid_error)
        except PIDUnregistered as pid_error:
            raise PIDUnregisteredRESTError(pid_error=pid_error)
        except PIDDeletedError as pid_error:
            raise PIDDeletedRESTError(pid_error=pid_error)
        except PIDMissingObjectError as pid_error:
            current_app.logger.exception(
                'No object assigned to {0}.'.format(pid_error.pid),
                extra={'pid': pid_error.pid})
            raise PIDMissingObjectRESTError(pid_error.pid, pid_error=pid_error)
        except PIDRedirectedError as pid_error:
            try:
                location = url_for(
                    '.{0}_item'.format(
                        current_records_rest.default_endpoint_prefixes[
                            pid_error.destination_pid.pid_type]),
                    pid_value=pid_error.destination_pid.pid_value)
                data = dict(
                    status=301,
                    message='Moved Permanently',
                    location=location,
                )
                response = make_response(jsonify(data), data['status'])
                response.headers['Location'] = location
                abort(response)
            except (BuildError, KeyError):
                current_app.logger.exception(
                    'Invalid redirect - pid_type "{0}" '
                    'endpoint missing.'.format(
                        pid_error.destination_pid.pid_type),
                    extra={
                        'pid': pid_error.pid,
                        'destination_pid': pid_error.destination_pid,
                    })
                raise PIDRedirectedRESTError(
                    pid_error.destination_pid.pid_type, pid_error=pid_error)


Instruction:
Implement a Python 3 function for
getting JSON dump indentation and separates.

Response:
def _format_args():
        """Get JSON dump indentation and separates."""
        if request and request.args.get('prettyprint'):
            return dict(
                indent=2,
                separators=(', ', ': '),
            )
        else:
            return dict(
                indent=None,
                separators=(',', ':'),
            )


Instruction:
Write a Python 3 script to
serialize a single record and persistent identifier.

Response:
def serialize(self, pid, record, links_factory=None, **kwargs):
        """Serialize a single record and persistent identifier.

        :param pid: Persistent identifier instance.
        :param record: Record instance.
        :param links_factory: Factory function for record links.
        """
        return json.dumps(
            self.transform_record(pid, record, links_factory, **kwargs),
            **self._format_args())


Instruction:
Can you tell what is the following Python 3 function doing
def serialize_search(self, pid_fetcher, search_result, links=None,
                         item_links_factory=None, **kwargs):
        """Serialize a search result.

        :param pid_fetcher: Persistent identifier fetcher.
        :param search_result: Elasticsearch search result.
        :param links: Dictionary of links to add to response.
        """
        return json.dumps(dict(
            hits=dict(
                hits=[self.transform_search_hit(
                    pid_fetcher(hit['_id'], hit['_source']),
                    hit,
                    links_factory=item_links_factory,
                    **kwargs
                ) for hit in search_result['hits']['hits']],
                total=search_result['hits']['total'],
            ),
            links=links or {},
            aggregations=search_result.get('aggregations', dict()),
        ), **self._format_args())

Response: Serialize a search result.


Instruction:
Given the following Python 3 function, write the documentation
def create_error_handlers(blueprint, error_handlers_registry=None):
    error_handlers_registry = error_handlers_registry or {}

    # Catch record validation errors
    @blueprint.errorhandler(ValidationError)
    def validation_error(error):
        """Catch validation errors."""
        return JSONSchemaValidationError(error=error).get_response()

    @blueprint.errorhandler(RequestError)
    def elasticsearch_badrequest_error(error):
        """Catch errors of ElasticSearch."""
        handlers = current_app.config[
            'RECORDS_REST_ELASTICSEARCH_ERROR_HANDLERS']
        cause_types = {c['type'] for c in error.info['error']['root_cause']}

        for cause_type, handler in handlers.items():
            if cause_type in cause_types:
                return handler(error)

        # Default exception for unhandled errors
        exception = UnhandledElasticsearchError()
        current_app.logger.exception(error)  # Log the original stacktrace
        return exception.get_response()

    for exc_or_code, handlers in error_handlers_registry.items():
        # Build full endpoint names and resolve handlers
        handlers = {
            '.'.join([blueprint.name, view_name]): obj_or_import_string(func)
            for view_name, func in handlers.items()
        }

        def dispatch_handler(error):
            def default_handler(e):
                raise e
            return handlers.get(request.endpoint, default_handler)(error)
        blueprint.register_error_handler(exc_or_code, dispatch_handler)

    return blueprint

Response: Create error handlers on Records API blueprint.


Instruction:
Here you have a function in Python 3, explain what it does
def create_blueprint(endpoints):
    endpoints = endpoints or {}

    blueprint = Blueprint(
        'invenio_records_rest',
        __name__,
        url_prefix='',
    )

    error_handlers_registry = defaultdict(dict)
    for endpoint, options in endpoints.items():
        error_handlers = options.pop('error_handlers', {})
        for rule in create_url_rules(endpoint, **options):
            for exc_or_code, handler in error_handlers.items():
                view_name = rule['view_func'].__name__
                error_handlers_registry[exc_or_code][view_name] = handler
            blueprint.add_url_rule(**rule)

    return create_error_handlers(blueprint, error_handlers_registry)

Response: Create Invenio - Records - REST blueprint.


Instruction:
Here you have a function in Python 3, explain what it does
def create_url_rules(endpoint, list_route=None, item_route=None,
                     pid_type=None, pid_minter=None, pid_fetcher=None,
                     read_permission_factory_imp=None,
                     create_permission_factory_imp=None,
                     update_permission_factory_imp=None,
                     delete_permission_factory_imp=None,
                     list_permission_factory_imp=None,
                     record_class=None,
                     record_serializers=None,
                     record_serializers_aliases=None,
                     record_loaders=None,
                     search_class=None,
                     indexer_class=RecordIndexer,
                     search_serializers=None,
                     search_index=None, search_type=None,
                     default_media_type=None,
                     max_result_window=None, use_options_view=True,
                     search_factory_imp=None, links_factory_imp=None,
                     suggesters=None, default_endpoint_prefix=None):
    """Create Werkzeug URL rules.

    :param endpoint: Name of endpoint.
    :param list_route: Record listing URL route. Required.
    :param item_route: Record URL route (must include ``<pid_value>`` pattern).
        Required.
    :param pid_type: Persistent identifier type for endpoint. Required.
    :param pid_minter: It identifies the registered minter name.
    :param pid_fetcher: It identifies the registered fetcher name.
    :param read_permission_factory_imp: Import path to factory that creates a
        read permission object for a given record.
    :param create_permission_factory_imp: Import path to factory that creates a
        create permission object for a given record.
    :param update_permission_factory_imp: Import path to factory that creates a
        update permission object for a given record.
    :param delete_permission_factory_imp: Import path to factory that creates a
        delete permission object for a given record.
    :param list_permission_factory_imp: Import path to factory that
        creates a list permission object for a given index/list.
    :param default_endpoint_prefix: ignored.
    :param record_class: A record API class or importable string used when
        creating new records.
    :param record_serializers: Serializers used for records.
    :param record_serializers_aliases: A mapping of query arg `format` values
        to valid mimetypes: dict(alias -> mimetype).
    :param record_loaders: It contains the list of record deserializers for
        supperted formats.
    :param search_class: Import path or class object for the object in charge
        of execute the search queries. The default search class is
        :class:`invenio_search.api.RecordsSearch`.
        For more information about resource loading, see the Search of
        ElasticSearch DSL library.
    :param indexer_class: Import path or class object for the object in charge
        of indexing records. The default indexer is
        :class:`invenio_indexer.api.RecordIndexer`.
    :param search_serializers: Serializers used for search results.
    :param search_index: Name of the search index used when searching records.
    :param search_type: Name of the search type used when searching records.
    :param default_media_type: Default media type for both records and search.
    :param max_result_window: Maximum number of results that Elasticsearch can
        provide for the given search index without use of scroll. This value
        should correspond to Elasticsearch ``index.max_result_window`` value
        for the index.
    :param use_options_view: Determines if a special option view should be
        installed.
    :param search_factory_imp: Factory to parse quieries.
    :param links_factory_imp: Factory for record links generation.
    :param suggesters: Suggester fields configuration.

    :returns: a list of dictionaries with can each be passed as keywords
        arguments to ``Blueprint.add_url_rule``.
    """
    assert list_route
    assert item_route
    assert pid_type
    assert search_serializers
    assert record_serializers

    read_permission_factory = obj_or_import_string(
        read_permission_factory_imp
    )
    create_permission_factory = obj_or_import_string(
        create_permission_factory_imp
    )
    update_permission_factory = obj_or_import_string(
        update_permission_factory_imp
    )
    delete_permission_factory = obj_or_import_string(
        delete_permission_factory_imp
    )
    list_permission_factory = obj_or_import_string(
        list_permission_factory_imp
    )
    links_factory = obj_or_import_string(
        links_factory_imp, default=default_links_factory
    )
    # For backward compatibility. Previous signature was links_factory(pid).
    if wrap_links_factory(links_factory):
        orig_links_factory = links_factory

        def links_factory(pid, record=None, **kwargs):
            return orig_links_factory(pid)

    record_class = obj_or_import_string(
        record_class, default=Record
    )
    search_class = obj_or_import_string(
        search_class, default=RecordsSearch
    )

    indexer_class = obj_or_import_string(
        indexer_class, default=None
    )

    search_class_kwargs = {}
    if search_index:
        search_class_kwargs['index'] = search_index
    else:
        search_index = search_class.Meta.index

    if search_type:
        search_class_kwargs['doc_type'] = search_type
    else:
        search_type = search_class.Meta.doc_types

    if search_class_kwargs:
        search_class = partial(search_class, **search_class_kwargs)

    if record_loaders:
        record_loaders = {mime: obj_or_import_string(func)
                          for mime, func in record_loaders.items()}
    record_serializers = {mime: obj_or_import_string(func)
                          for mime, func in record_serializers.items()}
    search_serializers = {mime: obj_or_import_string(func)
                          for mime, func in search_serializers.items()}

    list_view = RecordsListResource.as_view(
        RecordsListResource.view_name.format(endpoint),
        minter_name=pid_minter,
        pid_type=pid_type,
        pid_fetcher=pid_fetcher,
        read_permission_factory=read_permission_factory,
        create_permission_factory=create_permission_factory,
        list_permission_factory=list_permission_factory,
        record_serializers=record_serializers,
        record_loaders=record_loaders,
        search_serializers=search_serializers,
        search_class=search_class,
        indexer_class=indexer_class,
        default_media_type=default_media_type,
        max_result_window=max_result_window,
        search_factory=(obj_or_import_string(
            search_factory_imp, default=es_search_factory
        )),
        item_links_factory=links_factory,
        record_class=record_class,
    )
    item_view = RecordResource.as_view(
        RecordResource.view_name.format(endpoint),
        read_permission_factory=read_permission_factory,
        update_permission_factory=update_permission_factory,
        delete_permission_factory=delete_permission_factory,
        serializers=record_serializers,
        serializers_query_aliases=record_serializers_aliases,
        loaders=record_loaders,
        search_class=search_class,
        indexer_class=indexer_class,
        links_factory=links_factory,
        default_media_type=default_media_type)

    views = [
        dict(rule=list_route, view_func=list_view),
        dict(rule=item_route, view_func=item_view),
    ]
    if suggesters:
        suggest_view = SuggestResource.as_view(
            SuggestResource.view_name.format(endpoint),
            suggesters=suggesters,
            search_class=search_class,
        )

        views.append(dict(
            rule=list_route + '_suggest',
            view_func=suggest_view
        ))

    if use_options_view:
        options_view = RecordsListOptionsResource.as_view(
            RecordsListOptionsResource.view_name.format(endpoint),
            search_index=search_index,
            max_result_window=max_result_window,
            default_media_type=default_media_type,
            search_media_types=search_serializers.keys(),
            item_media_types=record_serializers.keys(),
        )
        return [
                   dict(rule="{0}_options".format(list_route),
                        view_func=options_view)
               ] + views
    return views

Response: Creates Werkzeug URL rules.


Instruction:
Can you tell what is the following Python 3 function doing
def verify_record_permission(permission_factory, record):
    # Note, cannot be done in one line due overloading of boolean
    # operations permission object.
    if not permission_factory(record=record).can():
        from flask_login import current_user
        if not current_user.is_authenticated:
            abort(401)
        abort(403)

Response: Verify that the current user has the required permissions on record.


Instruction:
Can you generate the documentation for the following Python 3 function
def need_record_permission(factory_name):
    def need_record_permission_builder(f):
        @wraps(f)
        def need_record_permission_decorator(self, record=None, *args,
                                             **kwargs):
            permission_factory = (
                getattr(self, factory_name) or
                getattr(current_records_rest, factory_name)
            )

            # FIXME use context instead
            request._methodview = self

            if permission_factory:
                verify_record_permission(permission_factory, record)
            return f(self, record=record, *args, **kwargs)
        return need_record_permission_decorator
    return need_record_permission_builder

Response: Decorator checking that the user has the required permissions on record.


Instruction:
Can you create a Python 3 function that
returns a Search result containing the records and aggregations as .

Response:
def get(self, **kwargs):
        """Search records.

        Permissions: the `list_permission_factory` permissions are
            checked.

        :returns: Search result containing hits and aggregations as
                  returned by invenio-search.
        """
        default_results_size = current_app.config.get(
            'RECORDS_REST_DEFAULT_RESULTS_SIZE', 10)
        page = request.values.get('page', 1, type=int)
        size = request.values.get('size', default_results_size, type=int)
        if page * size >= self.max_result_window:
            raise MaxResultWindowRESTError()

        # Arguments that must be added in prev/next links
        urlkwargs = dict()
        search_obj = self.search_class()
        search = search_obj.with_preference_param().params(version=True)
        search = search[(page - 1) * size:page * size]

        search, qs_kwargs = self.search_factory(search)
        urlkwargs.update(qs_kwargs)

        # Execute search
        search_result = search.execute()

        # Generate links for prev/next
        urlkwargs.update(
            size=size,
            _external=True,
        )
        endpoint = '.{0}_list'.format(
            current_records_rest.default_endpoint_prefixes[self.pid_type])
        links = dict(self=url_for(endpoint, page=page, **urlkwargs))
        if page > 1:
            links['prev'] = url_for(endpoint, page=page - 1, **urlkwargs)
        if size * page < search_result.hits.total and \
                size * page < self.max_result_window:
            links['next'] = url_for(endpoint, page=page + 1, **urlkwargs)

        return self.make_response(
            pid_fetcher=self.pid_fetcher,
            search_result=search_result.to_dict(),
            links=links,
            item_links_factory=self.item_links_factory,
        )


Instruction:
Write a Python 3 script to
create a record. Permissions: ``create_permission_factory`` Procedure description: #. First of all, the `create_permission_factory` permissions are checked. #. Then, the record is deserialized by the proper loader. #. A second call to the `create_permission_factory` factory is done: it differs from the previous call because this time the record is passed as parameter. #. A `uuid` is generated for the record and the minter is called. #. The record class is called to create the record. #. The HTTP response is built with the help of the item link factory. :returns: The created record.

Response:
def post(self, **kwargs):
        """Create a record.

        Permissions: ``create_permission_factory``

        Procedure description:

        #. First of all, the `create_permission_factory` permissions are
            checked.

        #. Then, the record is deserialized by the proper loader.

        #. A second call to the `create_permission_factory` factory is done:
            it differs from the previous call because this time the record is
            passed as parameter.

        #. A `uuid` is generated for the record and the minter is called.

        #. The record class is called to create the record.

        #. The HTTP response is built with the help of the item link factory.

        :returns: The created record.
        """
        if request.mimetype not in self.loaders:
            raise UnsupportedMediaRESTError(request.mimetype)

        data = self.loaders[request.mimetype]()
        if data is None:
            raise InvalidDataRESTError()

        # Check permissions
        permission_factory = self.create_permission_factory
        if permission_factory:
            verify_record_permission(permission_factory, data)

        # Create uuid for record
        record_uuid = uuid.uuid4()
        # Create persistent identifier
        pid = self.minter(record_uuid, data=data)
        # Create record
        record = self.record_class.create(data, id_=record_uuid)

        db.session.commit()

        # Index the record
        if self.indexer_class:
            self.indexer_class().index(record)

        response = self.make_response(
            pid, record, 201, links_factory=self.item_links_factory)

        # Add location headers
        endpoint = '.{0}_item'.format(
            current_records_rest.default_endpoint_prefixes[pid.pid_type])
        location = url_for(endpoint, pid_value=pid.pid_value, _external=True)
        response.headers.extend(dict(location=location))
        return response


Instruction:
Explain what the following Python 3 code does
def delete(self, pid, record, **kwargs):
        self.check_etag(str(record.model.version_id))

        record.delete()
        # mark all PIDs as DELETED
        all_pids = PersistentIdentifier.query.filter(
            PersistentIdentifier.object_type == pid.object_type,
            PersistentIdentifier.object_uuid == pid.object_uuid,
        ).all()
        for rec_pid in all_pids:
            if not rec_pid.is_deleted():
                rec_pid.delete()
        db.session.commit()
        if self.indexer_class:
            self.indexer_class().delete(record)

        return '', 204

Response:
Delete a record.

        Permissions: ``delete_permission_factory``

        Procedure description:

        #. The record is resolved reading the pid value from the url.

        #. The ETag is checked.

        #. The record is deleted.

        #. All PIDs are marked as DELETED.

        :param pid: Persistent identifier for record.
        :param record: Record object.


Instruction:
Write a Python 3 script for
getting a record. Permissions: ``read_permission_factory`` Procedure description: #. The record is resolved reading the pid value from the url. #. The ETag and If-Modifed-Since is checked. #. The HTTP response is built with the help of the link factory. :param pid: Persistent identifier for record. :param record: Record object. :returns: The requested record.

Response:
def get(self, pid, record, **kwargs):
        """Get a record.

        Permissions: ``read_permission_factory``

        Procedure description:

        #. The record is resolved reading the pid value from the url.

        #. The ETag and If-Modifed-Since is checked.

        #. The HTTP response is built with the help of the link factory.

        :param pid: Persistent identifier for record.
        :param record: Record object.
        :returns: The requested record.
        """
        etag = str(record.revision_id)
        self.check_etag(str(record.revision_id))
        self.check_if_modified_since(record.updated, etag=etag)

        return self.make_response(
            pid, record, links_factory=self.links_factory
        )


Instruction:
Create a Python 3 function to
modify a record. Permissions: ``update_permission_factory`` The data should be a JSON-patch, which will be applied to the record. Requires header ``Content-Type: application/json-patch+json``. Procedure description: #. The record is deserialized using the proper loader. #. The ETag is checked. #. The record is patched. #. The HTTP response is built with the help of the link factory. :param pid: Persistent identifier for record. :param record: Record object. :returns: The modified record.

Response:
def patch(self, pid, record, **kwargs):
        """Modify a record.

        Permissions: ``update_permission_factory``

        The data should be a JSON-patch, which will be applied to the record.
        Requires header ``Content-Type: application/json-patch+json``.

        Procedure description:

        #. The record is deserialized using the proper loader.

        #. The ETag is checked.

        #. The record is patched.

        #. The HTTP response is built with the help of the link factory.

        :param pid: Persistent identifier for record.
        :param record: Record object.
        :returns: The modified record.
        """
        data = self.loaders[request.mimetype]()
        if data is None:
            raise InvalidDataRESTError()

        self.check_etag(str(record.revision_id))
        try:
            record = record.patch(data)
        except (JsonPatchException, JsonPointerException):
            raise PatchJSONFailureRESTError()

        record.commit()
        db.session.commit()
        if self.indexer_class:
            self.indexer_class().index(record)

        return self.make_response(
            pid, record, links_factory=self.links_factory)


Instruction:
Can you generate a brief explanation for the following Python 3 code
def put(self, pid, record, **kwargs):
        if request.mimetype not in self.loaders:
            raise UnsupportedMediaRESTError(request.mimetype)

        data = self.loaders[request.mimetype]()
        if data is None:
            raise InvalidDataRESTError()

        self.check_etag(str(record.revision_id))

        record.clear()
        record.update(data)
        record.commit()
        db.session.commit()
        if self.indexer_class:
            self.indexer_class().index(record)
        return self.make_response(
            pid, record, links_factory=self.links_factory)

Response:
Replace a record.

        Permissions: ``update_permission_factory``

        The body should be a JSON object, which will fully replace the current
        record metadata.

        Procedure description:

        #. The ETag is checked.

        #. The record is updated by calling the record API `clear()`,
           `update()` and then `commit()`.

        #. The HTTP response is built with the help of the link factory.

        :param pid: Persistent identifier for record.
        :param record: Record object.
        :returns: The modified record.


Instruction:
Explain what the following Python 3 code does
def _serialize(self, value, attr, obj):
        try:
            return super(DateString, self)._serialize(
                arrow.get(value).date(), attr, obj)
        except ParserError:
            return missing

Response: Serialize an ISO8601 - formatted date.


Instruction:
Write a Python 3 function for
loading test data fixture.

Response:
def records():
    """Load test data fixture."""
    import uuid
    from invenio_records.api import Record
    from invenio_pidstore.models import PersistentIdentifier, PIDStatus

    indexer = RecordIndexer()
    index_queue = []

    # Record 1 - Live record
    with db.session.begin_nested():
        rec_uuid = uuid.uuid4()
        pid1 = PersistentIdentifier.create(
            'recid', '1', object_type='rec', object_uuid=rec_uuid,
            status=PIDStatus.REGISTERED)
        Record.create({
            'title': 'Registered',
            'description': 'This is an awesome description',
            # "mint" the record as recid minter does
            'control_number': '1',
        }, id_=rec_uuid)
        index_queue.append(pid1.object_uuid)

        # Record 2 - Deleted PID with record
        rec_uuid = uuid.uuid4()
        pid = PersistentIdentifier.create(
            'recid', '2', object_type='rec', object_uuid=rec_uuid,
            status=PIDStatus.REGISTERED)
        Record.create({
            'title': 'Live ',
            'control_number': '2',
        }, id_=rec_uuid)
        pid.delete()

        # Record 3 - Deleted PID without a record
        PersistentIdentifier.create(
            'recid', '3', status=PIDStatus.DELETED)

        # Record 4 - Registered PID without a record
        PersistentIdentifier.create(
            'recid', '4', status=PIDStatus.REGISTERED)

        # Record 5 - Redirected PID
        pid = PersistentIdentifier.create(
            'recid', '5', status=PIDStatus.REGISTERED)
        pid.redirect(pid1)

        # Record 6 - Redirected non existing endpoint
        doi = PersistentIdentifier.create(
            'doi', '10.1234/foo', status=PIDStatus.REGISTERED)
        pid = PersistentIdentifier.create(
            'recid', '6', status=PIDStatus.REGISTERED)
        pid.redirect(doi)

        # Record 7 - Unregistered PID
        PersistentIdentifier.create(
            'recid', '7', status=PIDStatus.RESERVED)

        for rec_idx in range(len(record_examples)):
            rec_uuid = uuid.uuid4()
            rec_pid = 8 + rec_idx
            pid1 = PersistentIdentifier.create(
                'recid', str(rec_pid), object_type='rec', object_uuid=rec_uuid,
                status=PIDStatus.REGISTERED)
            # "mint" the record as recid minter does
            record = dict(record_examples[rec_idx])
            record['control_number'] = str(rec_pid)
            # create the record
            Record.create(record, id_=rec_uuid)
            index_queue.append(rec_uuid)
    db.session.commit()

    for i in index_queue:
        indexer.index_by_id(i)


Instruction:
Here you have a function in Python 3, explain what it does
def preprocess_record(self, pid, record, links_factory=None, **kwargs):
        links_factory = links_factory or (lambda x, record=None, **k: dict())
        metadata = copy.deepcopy(record.replace_refs()) if self.replace_refs \
            else record.dumps()
        return dict(
            pid=pid,
            metadata=metadata,
            links=links_factory(pid, record=record, **kwargs),
            revision=record.revision_id,
            created=(pytz.utc.localize(record.created).isoformat()
                     if record.created else None),
            updated=(pytz.utc.localize(record.updated).isoformat()
                     if record.updated else None),
        )

Response: Prepare a record and persistent identifier for serialization.


Instruction:
Implement a Python 3 function for
preparing a record hit from Elasticsearch for serialization.

Response:
def preprocess_search_hit(pid, record_hit, links_factory=None, **kwargs):
        """Prepare a record hit from Elasticsearch for serialization."""
        links_factory = links_factory or (lambda x, **k: dict())
        record = dict(
            pid=pid,
            metadata=record_hit['_source'],
            links=links_factory(pid, record_hit=record_hit, **kwargs),
            revision=record_hit['_version'],
            created=None,
            updated=None,
        )
        # Move created/updated attrs from source to object.
        for key in ['_created', '_updated']:
            if key in record['metadata']:
                record[key[1:]] = record['metadata'][key]
                del record['metadata'][key]
        return record


Instruction:
Can you implement a function in Python 3 that
gets the request body.

Response:
def get_body(self, environ=None):
        """Get the request body."""
        body = dict(
            status=self.code,
            message=self.get_description(environ),
        )

        if self.errors:
            body['errors'] = self.errors

        return json.dumps(body)


Instruction:
Given the following Python 3 function, write the documentation
def reset_permission_factories(self):
        for key in ('read', 'create', 'update', 'delete'):
            full_key = '{0}_permission_factory'.format(key)
            if full_key in self.__dict__:
                del self.__dict__[full_key]

Response: Remove cached permission factories.


Instruction:
Can you write a function in Python 3 where it
initializes the Flask application.

Response:
def init_app(self, app):
        """Flask application initialization."""
        self.init_config(app)
        app.extensions['invenio-records-rest'] = _RecordRESTState(app)


Instruction:
Create a Python 3 function to
create a range filter.

Response:
def range_filter(field, start_date_math=None, end_date_math=None, **kwargs):
    """Create a range filter.

    :param field: Field name.
    :param start_date_math: Starting date.
    :param end_date_math: Ending date.
    :param kwargs: Addition arguments passed to the Range query.
    :returns: Function that returns the Range query.
    """
    def inner(values):
        if len(values) != 1 or values[0].count('--') != 1 or values[0] == '--':
            raise RESTValidationError(
                errors=[FieldError(field, 'Invalid range format.')])

        range_ends = values[0].split('--')
        range_args = dict()

        ineq_opers = [{'strict': 'gt', 'nonstrict': 'gte'},
                      {'strict': 'lt', 'nonstrict': 'lte'}]
        date_maths = [start_date_math, end_date_math]

        # Add the proper values to the dict
        for (range_end, strict, opers,
             date_math) in zip(range_ends, ['>', '<'], ineq_opers, date_maths):

            if range_end != '':
                # If first char is '>' for start or '<' for end
                if range_end[0] == strict:
                    dict_key = opers['strict']
                    range_end = range_end[1:]
                else:
                    dict_key = opers['nonstrict']

                if date_math:
                    range_end = '{0}||{1}'.format(range_end, date_math)

                range_args[dict_key] = range_end

        args = kwargs.copy()
        args.update(range_args)

        return Range(**{field: args})

    return inner


Instruction:
Can you generate the documentation for the following Python 3 function
def _create_filter_dsl(urlkwargs, definitions):
    filters = []
    for name, filter_factory in definitions.items():
        values = request.values.getlist(name, type=text_type)
        if values:
            filters.append(filter_factory(values))
            for v in values:
                urlkwargs.add(name, v)

    return (filters, urlkwargs)

Response: Create a filter DSL expression.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def _post_filter(search, urlkwargs, definitions):
    filters, urlkwargs = _create_filter_dsl(urlkwargs, definitions)

    for filter_ in filters:
        search = search.post_filter(filter_)

    return (search, urlkwargs)

Response: Ingest post filter in query.


Instruction:
How would you explain what the following Python 3 function does
def _query_filter(search, urlkwargs, definitions):
    filters, urlkwargs = _create_filter_dsl(urlkwargs, definitions)

    for filter_ in filters:
        search = search.filter(filter_)

    return (search, urlkwargs)

Response: Ingest query filter in query.


Instruction:
Make a summary of the following Python 3 code
def _aggregations(search, definitions):
    if definitions:
        for name, agg in definitions.items():
            search.aggs[name] = agg if not callable(agg) else agg()
    return search

Response: Add aggregations to query.


Instruction:
How would you explain what the following Python 3 function does
def default_facets_factory(search, index):
    urlkwargs = MultiDict()

    facets = current_app.config['RECORDS_REST_FACETS'].get(index)

    if facets is not None:
        # Aggregations.
        search = _aggregations(search, facets.get("aggs", {}))

        # Query filter
        search, urlkwargs = _query_filter(
            search, urlkwargs, facets.get("filters", {}))

        # Post filter
        search, urlkwargs = _post_filter(
            search, urlkwargs, facets.get("post_filters", {}))

    return (search, urlkwargs)

Response: Add a default facets to query.


Instruction:
Make a summary of the following Python 3 code
def dump(self, obj, context=None):
        return self.schema_class(context=context).dump(obj).data

Response: Serialize object with schema.


Instruction:
How would you implement a function in Python 3 that
transforms record into an intermediate representation.

Response:
def transform_record(self, pid, record, links_factory=None, **kwargs):
        """Transform record into an intermediate representation."""
        context = kwargs.get('marshmallow_context', {})
        context.setdefault('pid', pid)
        return self.dump(self.preprocess_record(pid, record,
                         links_factory=links_factory, **kwargs), context)


Instruction:
Can you generate a brief explanation for the following Python 3 code
def transform_search_hit(self, pid, record_hit, links_factory=None,
                             **kwargs):
        """Transform search result hit into an intermediate representation."""
        context = kwargs.get('marshmallow_context', {})
        context.setdefault('pid', pid)
        return self.dump(self.preprocess_search_hit(pid, record_hit,
                         links_factory=links_factory, **kwargs), context)

Response: Transform search result hit into an intermediate representation.


Instruction:
Can you tell what is the following Python 3 function doing
def pid_from_context(_, context):
    pid = (context or {}).get('pid')
    return pid.pid_value if pid else missing

Response: Get PID from marshmallow context.


Instruction:
Can you generate the documentation for the following Python 3 function
def record_responsify(serializer, mimetype):
    def view(pid, record, code=200, headers=None, links_factory=None):
        response = current_app.response_class(
            serializer.serialize(pid, record, links_factory=links_factory),
            mimetype=mimetype)
        response.status_code = code
        response.set_etag(str(record.revision_id))
        response.last_modified = record.updated
        if headers is not None:
            response.headers.extend(headers)

        if links_factory is not None:
            add_link_header(response, links_factory(pid))

        return response

    return view

Response: Create a Records - REST response serializer.


Instruction:
Make a summary of the following Python 3 code
def search_responsify(serializer, mimetype):
    def view(pid_fetcher, search_result, code=200, headers=None, links=None,
             item_links_factory=None):
        response = current_app.response_class(
            serializer.serialize_search(pid_fetcher, search_result,
                                        links=links,
                                        item_links_factory=item_links_factory),
            mimetype=mimetype)
        response.status_code = code
        if headers is not None:
            response.headers.extend(headers)

        if links is not None:
            add_link_header(response, links)

        return response

    return view

Response: Create a Records - REST search result response serializer.


Instruction:
Explain what the following Python 3 code does
def add_link_header(response, links):
    if links is not None:
        response.headers.extend({
            'Link': ', '.join([
                '<{0}>; rel="{1}"'.format(l, r) for r, l in links.items()])
        })

Response: Add a Link HTTP header to a REST response instance.


Instruction:
How would you implement a function in Python 3 that
parses style and locale.

Response:
def _get_args(cls, **kwargs):
        """Parse style and locale.

        Argument location precedence: kwargs > view_args > query
        """
        csl_args = {
            'style': cls._default_style,
            'locale': cls._default_locale
        }

        if has_request_context():
            parser = FlaskParser(locations=('view_args', 'query'))
            csl_args.update(parser.parse(cls._user_args, request))

        csl_args.update({k: kwargs[k]
                         for k in ('style', 'locale') if k in kwargs})

        try:
            csl_args['style'] = get_style_filepath(csl_args['style'].lower())
        except StyleNotFoundError:
            if has_request_context():
                raise StyleNotFoundRESTError(csl_args['style'])
            raise
        return csl_args


Instruction:
How would you implement a function in Python 3 that
gets source data object for citeproc - py.

Response:
def _get_source(self, data):
        """Get source data object for citeproc-py."""
        if self.record_format == 'csl':
            return CiteProcJSON([json.loads(data)])
        elif self.record_format == 'bibtex':
            return BibTeX(data)


Instruction:
How would you explain what the following Python 3 function does
def _clean_result(self, text):
        text = re.sub('\s\s+', ' ', text)
        text = re.sub('\.\.+', '.', text)
        text = text.replace("'", "\\'")
        return text

Response: Remove double spaces punctuation and escapes apostrophes.


Instruction:
Write a Python 3 script for
serializing a single record.

Response:
def serialize(self, pid, record, links_factory=None, **kwargs):
        """Serialize a single record.

        :param pid: Persistent identifier instance.
        :param record: Record instance.
        :param links_factory: Factory function for record links.
        """
        data = self.serializer.serialize(pid, record, links_factory)
        source = self._get_source(data)
        style = CitationStylesStyle(validate=False, **self._get_args(**kwargs))
        bib = CitationStylesBibliography(style, source, formatter.plain)
        citation = Citation([CitationItem(pid.pid_value)])
        bib.register(citation)

        return self._clean_result(''.join(bib.bibliography()[0]))


Instruction:
Can you implement a function in Python 3 that
checks if a character is valid based on the XML specification.

Response:
def is_valid_xml_char(self, char):
        """Check if a character is valid based on the XML specification."""
        codepoint = ord(char)
        return (0x20 <= codepoint <= 0xD7FF or
                codepoint in (0x9, 0xA, 0xD) or
                0xE000 <= codepoint <= 0xFFFD or
                0x10000 <= codepoint <= 0x10FFFF)


Instruction:
Write a Python 3 script for
serializing a single record and persistent identifier.

Response:
def serialize(self, pid, record, links_factory=None):
        """Serialize a single record and persistent identifier.

        :param pid: Persistent identifier instance.
        :param record: Record instance.
        :param links_factory: Factory function for record links.
        """
        return self.schema.tostring(
            self.transform_record(pid, record, links_factory))


Instruction:
Write a Python 3 script for
serializing a search result.

Response:
def serialize_search(self, pid_fetcher, search_result, links=None,
                         item_links_factory=None):
        """Serialize a search result.

        :param pid_fetcher: Persistent identifier fetcher.
        :param search_result: Elasticsearch search result.
        :param links: Dictionary of links to add to response.
        """
        records = []
        for hit in search_result['hits']['hits']:
            records.append(self.schema.tostring(self.transform_search_hit(
                pid_fetcher(hit['_id'], hit['_source']),
                hit,
                links_factory=item_links_factory,
            )))

        return "\n".join(records)


Instruction:
Write a Python 3 function for
serializing a single record for OAI - PMH.

Response:
def serialize_oaipmh(self, pid, record):
        """Serialize a single record for OAI-PMH."""
        obj = self.transform_record(pid, record['_source']) \
            if isinstance(record['_source'], Record) \
            else self.transform_search_hit(pid, record)

        return self.schema.dump_etree(obj)


Instruction:
Given the following Python 3 function, write the documentation
def serialize_oaipmh(self, pid, record):
        root = etree.Element(
            'oai_datacite',
            nsmap={
                None: 'http://schema.datacite.org/oai/oai-1.0/',
                'xsi': 'http://www.w3.org/2001/XMLSchema-instance',
                'xml': 'xml',
            },
            attrib={
                '{http://www.w3.org/2001/XMLSchema-instance}schemaLocation':
                'http://schema.datacite.org/oai/oai-1.0/ oai_datacite.xsd',
            }
        )

        root.append(E.isReferenceQuality(self.is_reference_quality))
        root.append(E.schemaVersion(self.serializer.version))
        root.append(E.datacentreSymbol(self.datacentre))
        root.append(E.payload(
            self.serializer.serialize_oaipmh(pid, record)
        ))

        return root

Response: Serialize a single record for OAI - PMH.


Instruction:
Here you have a function in Python 3, explain what it does
def default_search_factory(self, search, query_parser=None):
    def _default_parser(qstr=None):
        """Default parser that uses the Q() from elasticsearch_dsl."""
        if qstr:
            return Q('query_string', query=qstr)
        return Q()

    from .facets import default_facets_factory
    from .sorter import default_sorter_factory

    query_string = request.values.get('q')
    query_parser = query_parser or _default_parser

    try:
        search = search.query(query_parser(query_string))
    except SyntaxError:
        current_app.logger.debug(
            "Failed parsing query: {0}".format(
                request.values.get('q', '')),
            exc_info=True)
        raise InvalidQueryRESTError()

    search_index = search._index[0]
    search, urlkwargs = default_facets_factory(search, search_index)
    search, sortkwargs = default_sorter_factory(search, search_index)
    for key, value in sortkwargs.items():
        urlkwargs.add(key, value)

    urlkwargs.add('q', query_string)
    return search, urlkwargs

Response: Parse query using elasticsearch DSL query.


Instruction:
Write a Python 3 script for
getting a list of the arguments a function or method has.

Response:
def _get_func_args(func):
    """Get a list of the arguments a function or method has."""
    if isinstance(func, functools.partial):
        return _get_func_args(func.func)
    if inspect.isfunction(func) or inspect.ismethod(func):
        return list(inspect.getargspec(func).args)
    if callable(func):
        return list(inspect.getargspec(func.__call__).args)


Instruction:
Can you generate a brief explanation for the following Python 3 code
def serialize(self, pid, record, links_factory=None):
        return simpledc.tostring(
            self.transform_record(pid, record, links_factory))

Response: Serialize a single record and persistent identifier.


Instruction:
Write a Python 3 script for
getting JSON - LD expanded state.

Response:
def expanded(self):
        """Get JSON-LD expanded state."""
        # Ensure we can run outside a application/request context.
        if request:
            if 'expanded' in request.args:
                return True
            elif 'compacted' in request.args:
                return False
        return self._expanded


Instruction:
Can you create a Python 3 function that
compacts JSON according to context.

Response:
def transform_jsonld(self, obj):
        """Compact JSON according to context."""
        rec = copy.deepcopy(obj)
        rec.update(self.context)
        compacted = jsonld.compact(rec, self.context)
        if not self.expanded:
            return compacted
        else:
            return jsonld.expand(compacted)[0]


Instruction:
Write a Python 3 script for
transforming record into an intermediate representation.

Response:
def transform_record(self, pid, record, links_factory=None, **kwargs):
        """Transform record into an intermediate representation."""
        result = super(JSONLDTransformerMixin, self).transform_record(
            pid, record, links_factory, **kwargs
        )
        return self.transform_jsonld(result)


Instruction:
Write a Python 3 function that can
transform search result hit into an intermediate representation.

Response:
def transform_search_hit(self, pid, record_hit, links_factory=None,
                             **kwargs):
        """Transform search result hit into an intermediate representation."""
        result = super(JSONLDTransformerMixin, self).transform_search_hit(
            pid, record_hit, links_factory, **kwargs
        )
        return self.transform_jsonld(result)


Instruction:
Explain what the following Python 3 code does
def default_links_factory_with_additional(additional_links):
    def factory(pid, **kwargs):
        links = default_links_factory(pid)
        for link in additional_links:
            links[link] = additional_links[link].format(pid=pid,
                                                        scheme=request.scheme,
                                                        host=request.host)
        return links

    return factory

Response: Generate a links generation factory with the specified additional links.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def geolocation_sort(field_name, argument, unit, mode=None,
                     distance_type=None):
    """Sort field factory for geo-location based sorting.

    :param argument: Name of URL query string field to parse pin location from.
        Multiple locations can be provided. Each location can be either a
        string "latitude,longitude" or a geohash.
    :param unit: Distance unit (e.g. km).
    :param mode: Sort mode (avg, min, max).
    :param distance_type: Distance calculation mode.
    :returns: Function that returns geolocation sort field.
    """
    def inner(asc):
        locations = request.values.getlist(argument, type=str)
        field = {
            '_geo_distance': {
                field_name: locations,
                'order': 'asc' if asc else 'desc',
                'unit': unit,
            }
        }
        if mode:
            field['_geo_distance']['mode'] = mode
        if distance_type:
            field['_geo_distance']['distance_type'] = distance_type
        return field
    return inner

Response: Returns a function that returns a geo - location based sorting field.


Instruction:
Explain what the following Python 3 code does
def eval_field(field, asc):
    if isinstance(field, dict):
        if asc:
            return field
        else:
            # Field should only have one key and must have an order subkey.
            field = copy.deepcopy(field)
            key = list(field.keys())[0]
            field[key]['order'] = reverse_order(field[key]['order'])
            return field
    elif callable(field):
        return field(asc)
    else:
        key, key_asc = parse_sort_field(field)
        if not asc:
            key_asc = not key_asc
        return {key: {'order': 'asc' if key_asc else 'desc'}}

Response: Evaluate a field for sorting purpose.


Instruction:
Can you create a Python 3 function that
defaults sort query factory.

Response:
def default_sorter_factory(search, index):
    """Default sort query factory.

    :param query: Search query.
    :param index: Index to search in.
    :returns: Tuple of (query, URL arguments).
    """
    sort_arg_name = 'sort'
    urlfield = request.values.get(sort_arg_name, '', type=str)

    # Get default sorting if sort is not specified.
    if not urlfield:
        # cast to six.text_type to handle unicodes in Python 2
        has_query = request.values.get('q', type=six.text_type)
        urlfield = current_app.config['RECORDS_REST_DEFAULT_SORT'].get(
            index, {}).get('query' if has_query else 'noquery', '')

    # Parse sort argument
    key, asc = parse_sort_field(urlfield)

    # Get sort options
    sort_options = current_app.config['RECORDS_REST_SORT_OPTIONS'].get(
        index, {}).get(key)
    if sort_options is None:
        return (search, {})

    # Get fields to sort query by
    search = search.sort(
        *[eval_field(f, asc) for f in sort_options['fields']]
    )
    return (search, {sort_arg_name: urlfield})


Instruction:
Explain what the following Python 3 code does
def check_unknown_fields(self, data, original_data):
        if isinstance(original_data, list):
            for elem in original_data:
                self.check_unknown_fields(data, elem)
        else:
            for key in original_data:
                if key not in [
                        self.fields[field].attribute or field
                        for field in self.fields
                ]:
                    raise ValidationError(
                        'Unknown field name {}'.format(key), field_names=[key])

Response: Check for unknown keys.


Instruction:
Can you tell what is the following Python 3 function doing
def load_unknown_fields(self, data, original_data):
        if isinstance(original_data, list):
            for elem in original_data:
                self.load_unknown_fields(data, elem)
        else:
            for key, value in original_data.items():
                if key not in data:
                    data[key] = value
        return data

Response: Check for unknown keys.


Instruction:
Can you tell what is the following Python 3 function doing
def inject_pid(self, data):
        # Remove already deserialized "pid" field
        pid_value = data.pop('pid', None)
        if pid_value:
            pid_field = current_app.config['PIDSTORE_RECID_FIELD']
            data.setdefault(pid_field, pid_value)
        return data

Response: Inject the context PID in the RECID field.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def as_uni_form(form):
    if isinstance(form, BaseFormSet):
        if settings.DEBUG:
            template = get_template('uni_form/uni_formset.html')
        else:
            template = uni_formset_template
        c = Context({'formset': form})
    else:
        if settings.DEBUG:
            template = get_template('uni_form/uni_form.html')
        else:
            template = uni_form_template
        c = Context({'form': form})
    return template.render(c)

Response: Returns a string that can be used to generate a uni - form for a given formset.


Instruction:
Can you create a Python 3 function that
returns a string that can be used to display the form errors in the Uni - Forms page.

Response:
def as_uni_errors(form):
    """
    Renders only form errors like django-uni-form::

        {% load uni_form_tags %}
        {{ form|as_uni_errors }}
    """
    if isinstance(form, BaseFormSet):
        template = get_template('uni_form/errors_formset.html')
        c = Context({'formset': form})
    else:
        template = get_template('uni_form/errors.html')
        c = Context({'form':form})
    return template.render(c)


Instruction:
How would you explain what the following Python 3 function does
def as_uni_field(field):
    template = get_template('uni_form/field.html')
    c = Context({'field':field})
    return template.render(c)

Response: Returns a Django - uni - form field as a string.


Instruction:
How would you explain what the following Python 3 function does
def render(self, form, form_style, context):
        return render_to_string(self.template, Context({'input': self}))

Response: Renders the input for the current object.


Instruction:
Write a Python 3 function for
rendering a field using the given form and context.

Response:
def render_field(field, form, form_style, context, template=None, labelclass=None, layout_object=None):
    """
    Renders a django-uni-form field

    :param field: Can be a string or a Layout object like `Row`. If it's a layout
        object, we call its render method, otherwise we instantiate a BoundField
        and render it using default template 'uni_form/field.html'
        The field is added to a list that the form holds called `rendered_fields`
        to avoid double rendering fields.

    :param form: The form/formset to which that field belongs to.

    :param form_style: We need this to render uni-form divs using helper's chosen
        style.

    :template: Template used for rendering the field.

    :layout_object: If passed, it points to the Layout object that is being rendered.
        We use it to store its bound fields in a list called `layout_object.bound_fields`
    """
    FAIL_SILENTLY = getattr(settings, 'UNIFORM_FAIL_SILENTLY', True)

    if hasattr(field, 'render'):
        return field.render(form, form_style, context)
    else:
        # This allows fields to be unicode strings, always they don't use non ASCII
        try:
            if isinstance(field, unicode):
                field = str(field)
            # If `field` is not unicode then we turn it into a unicode string, otherwise doing
            # str(field) would give no error and the field would not be resolved, causing confusion
            else:
                field = str(unicode(field))

        except (UnicodeEncodeError, UnicodeDecodeError):
            raise Exception("Field '%s' is using forbidden unicode characters" % field)

    try:
        field_instance = form.fields[field]
    except KeyError:
        if not FAIL_SILENTLY:
            raise Exception("Could not resolve form field '%s'." % field)
        else:
            field_instance = None
            logging.warning("Could not resolve form field '%s'." % field, exc_info=sys.exc_info())

    if not field in form.rendered_fields:
        form.rendered_fields.append(field)
    else:
        if not FAIL_SILENTLY:
            raise Exception("A field should only be rendered once: %s" % field)
        else:
            logging.warning("A field should only be rendered once: %s" % field, exc_info=sys.exc_info())

    if field_instance is None:
        html = ''
    else:
        bound_field = BoundField(form, field_instance, field)

        if template is None:
            template = default_field_template
        else:
            template = get_template(template)

        # We save the Layout object's bound fields in the layout object's `bound_fields` list
        if layout_object is not None:
            layout_object.bound_fields.append(bound_field)

        html = template.render(Context({'field': bound_field, 'labelclass': labelclass}))

    return html


Instruction:
Can you generate the documentation for the following Python 3 function
def do_uni_form(parser, token):
    token = token.split_contents()
    form = token.pop(1)

    try:
        helper = token.pop(1)
    except IndexError:
        helper = None

    return UniFormNode(form, helper)

Response: Handles uni - form tagging.


Instruction:
How would you code a function in Python 3 to
return a Context object with all the necesarry stuff for rendering the form.

Response:
def get_render(self, context):
        """
        Returns a `Context` object with all the necesarry stuff for rendering the form

        :param context: `django.template.Context` variable holding the context for the node

        `self.form` and `self.helper` are resolved into real Python objects resolving them
        from the `context`. The `actual_form` can be a form or a formset. If it's a formset
        `is_formset` is set to True. If the helper has a layout we use it, for rendering the
        form or the formset's forms.
        """
        actual_form = self.form.resolve(context)
        attrs = {}
        if self.helper is not None:
            helper = self.helper.resolve(context)

            if not isinstance(helper, FormHelper):
                raise TypeError('helper object provided to uni_form tag must be a uni_form.helpers.FormHelper object.')
            attrs = helper.get_attributes()
        else:
            helper = None

        # We get the response dictionary
        is_formset = isinstance(actual_form, BaseFormSet)
        response_dict = self.get_response_dict(attrs, context, is_formset)

        # If we have a helper's layout we use it, for the form or the formset's forms
        if helper and helper.layout:
            if not is_formset:
                actual_form.form_html = helper.render_layout(actual_form, context)
            else:
                forloop = ForLoopSimulator(actual_form)
                for form in actual_form.forms:
                    context.update({'forloop': forloop})
                    form.form_html = helper.render_layout(form, context)
                    forloop.iterate()

        if is_formset:
            response_dict.update({'formset': actual_form})
        else:
            response_dict.update({'form': actual_form})

        return Context(response_dict)


Instruction:
Can you tell what is the following Python 3 function doing
def get_response_dict(self, attrs, context, is_formset):
        form_type = "form"
        if is_formset:
            form_type = "formset"

        # We take form/formset parameters from attrs if they are set, otherwise we use defaults
        response_dict = {
            '%s_action' % form_type: attrs.get("form_action", ''),
            '%s_method' % form_type: attrs.get("form_method", 'post'),
            '%s_tag' % form_type: attrs.get("form_tag", True),
            '%s_class' % form_type: attrs.get("class", ''),
            '%s_id' % form_type: attrs.get("id", ""),
            '%s_style' % form_type: attrs.get("form_style", None),
            'form_error_title': attrs.get("form_error_title", None),
            'formset_error_title': attrs.get("formset_error_title", None),
            'inputs': attrs.get('inputs', []),
            'is_formset': is_formset,
        }

        if context.has_key('csrf_token'):
            response_dict['csrf_token'] = context['csrf_token']

        return response_dict

Response: Returns a dictionary with all the parameters necessary to render the formset in a template.


Instruction:
Can you generate the documentation for the following Python 3 function
def render_layout(self, form, context):
        form.rendered_fields = []

        html = self.layout.render(form, self.form_style, context)

        for field in form.fields.keys():
            if not field in form.rendered_fields:
                html += render_field(field, form, self.form_style, context)

        return mark_safe(html)

Response: Returns safe html of the rendering of the layout


Instruction:
Make a summary of the following Python 3 code
def get_attributes(self):
        items = {}
        items['form_method'] = self.form_method.strip()
        items['form_tag'] = self.form_tag
        items['form_style'] = self.form_style.strip()

        if self.form_action:
            items['form_action'] = self.form_action.strip()
        if self.form_id:
            items['id'] = self.form_id.strip()
        if self.form_class:
            items['class'] = self.form_class.strip()
        if self.inputs:
            items['inputs'] = self.inputs
        if self.form_error_title:
            items['form_error_title'] = self.form_error_title.strip()
        if self.formset_error_title:
            items['formset_error_title'] = self.formset_error_title.strip()
        return items

Response: Returns a dictionary of all the attributes that are set in the current object.


Instruction:
Create a Python 3 function for
functioning to add the counts for each sample in the group

Response:
def add_exp(self,gr,exp):
        """Function to add the counts for each sample

        :param gr: name of the sample
        :param exp: counts of sample **gr**

        :returns: dict with key,values equally to name,counts.
        """
        self.group[gr] = exp
        self.total = sum(self.group.values())


Instruction:
How would you code a function in Python 3 to
make a joint distribution of values from pmf1 and pmf2.

Response:
def MakeJoint(pmf1, pmf2):
    """Joint distribution of values from pmf1 and pmf2.

    Args:
        pmf1: Pmf object
        pmf2: Pmf object

    Returns:
        Joint pmf of value pairs
    """
    joint = Joint()
    for v1, p1 in pmf1.Items():
        for v2, p2 in pmf2.Items():
            joint.Set((v1, v2), p1 * p2)
    return joint


Instruction:
Given the following Python 3 function, write the documentation
def MakeHistFromList(t, name=''):
    hist = Hist(name=name)
    [hist.Incr(x) for x in t]
    return hist

Response: Makes a histogram from an unsorted sequence of numbers.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def MakePmfFromList(t, name=''):
    hist = MakeHistFromList(t)
    d = hist.GetDict()
    pmf = Pmf(d, name)
    pmf.Normalize()
    return pmf

Response: Makes a PMF from an unsorted sequence of numbers.


Instruction:
How would you explain what the following Python 3 function does
def MakePmfFromDict(d, name=''):
    pmf = Pmf(d, name)
    pmf.Normalize()
    return pmf

Response: Makes a PMF from a dictionary that maps values to probabilities.


Instruction:
Here you have a function in Python 3, explain what it does
def MakePmfFromItems(t, name=''):
    pmf = Pmf(dict(t), name)
    pmf.Normalize()
    return pmf

Response: Makes a PMF from a sequence of value - probability pairs.


Instruction:
Can you write a function in Python 3 where it
makes a normalized PMF from a Hist object.

Response:
def MakePmfFromHist(hist, name=None):
    """Makes a normalized PMF from a Hist object.

    Args:
        hist: Hist object
        name: string name

    Returns:
        Pmf object
    """
    if name is None:
        name = hist.name

    # make a copy of the dictionary
    d = dict(hist.GetDict())
    pmf = Pmf(d, name)
    pmf.Normalize()
    return pmf


Instruction:
Can you implement a function in Python 3 that
makes a normalized Pmf from a Cdf object.

Response:
def MakePmfFromCdf(cdf, name=None):
    """Makes a normalized Pmf from a Cdf object.

    Args:
        cdf: Cdf object
        name: string name for the new Pmf

    Returns:
        Pmf object
    """
    if name is None:
        name = cdf.name

    pmf = Pmf(name=name)

    prev = 0.0
    for val, prob in cdf.Items():
        pmf.Incr(val, prob - prev)
        prev = prob

    return pmf


Instruction:
Can you implement a function in Python 3 that
makes a mixture distribution.

Response:
def MakeMixture(metapmf, name='mix'):
    """Make a mixture distribution.

    Args:
      metapmf: Pmf that maps from Pmfs to probs.
      name: string name for the new Pmf.

    Returns: Pmf object.
    """
    mix = Pmf(name=name)
    for pmf, p1 in metapmf.Items():
        for x, p2 in pmf.Items():
            mix.Incr(x, p1 * p2)
    return mix


Instruction:
Implement a Python 3 function for
making a uniform Pmf.

Response:
def MakeUniformPmf(low, high, n):
    """Make a uniform Pmf.

    low: lowest value (inclusive)
    high: highest value (inclusize)
    n: number of values
    """
    pmf = Pmf()
    for x in numpy.linspace(low, high, n):
        pmf.Set(x, 1)
    pmf.Normalize()
    return pmf


Instruction:
Can you implement a function in Python 3 that
makes a cdf from an unsorted sequence of ( value frequency ) pairs.

Response:
def MakeCdfFromItems(items, name=''):
    """Makes a cdf from an unsorted sequence of (value, frequency) pairs.

    Args:
        items: unsorted sequence of (value, frequency) pairs
        name: string name for this CDF

    Returns:
        cdf: list of (value, fraction) pairs
    """
    runsum = 0
    xs = []
    cs = []

    for value, count in sorted(items):
        runsum += count
        xs.append(value)
        cs.append(runsum)

    total = float(runsum)
    ps = [c / total for c in cs]

    cdf = Cdf(xs, ps, name)
    return cdf


Instruction:
Can you tell what is the following Python 3 function doing
def MakeCdfFromPmf(pmf, name=None):
    if name == None:
        name = pmf.name
    return MakeCdfFromItems(pmf.Items(), name)

Response: Makes a CDF from a Pmf object.


Instruction:
Given the following Python 3 function, write the documentation
def MakeSuiteFromList(t, name=''):
    hist = MakeHistFromList(t)
    d = hist.GetDict()
    return MakeSuiteFromDict(d)

Response: Makes a suite from an unsorted sequence of values.


Instruction:
Create a Python 3 function to
make a normalized suite from a Hist object.

Response:
def MakeSuiteFromHist(hist, name=None):
    """Makes a normalized suite from a Hist object.

    Args:
        hist: Hist object
        name: string name

    Returns:
        Suite object
    """
    if name is None:
        name = hist.name

    # make a copy of the dictionary
    d = dict(hist.GetDict())
    return MakeSuiteFromDict(d, name)


Instruction:
Can you generate the documentation for the following Python 3 function
def MakeSuiteFromDict(d, name=''):
    suite = Suite(name=name)
    suite.SetDict(d)
    suite.Normalize()
    return suite

Response: Makes a suite from a dictionary that maps values to probabilities.


Instruction:
Can you create a Python 3 function that
makes a normalized Suite from a Cdf object.

Response:
def MakeSuiteFromCdf(cdf, name=None):
    """Makes a normalized Suite from a Cdf object.

    Args:
        cdf: Cdf object
        name: string name for the new Suite

    Returns:
        Suite object
    """
    if name is None:
        name = cdf.name

    suite = Suite(name=name)

    prev = 0.0
    for val, prob in cdf.Items():
        suite.Incr(val, prob - prev)
        prev = prob

    return suite


Instruction:
Can you implement a function in Python 3 that
computes a percentile of a given Pmf.

Response:
def Percentile(pmf, percentage):
    """Computes a percentile of a given Pmf.

    percentage: float 0-100
    """
    p = percentage / 100.0
    total = 0
    for val, prob in pmf.Items():
        total += prob
        if total >= p:
            return val


Instruction:
Make a summary of the following Python 3 code
def CredibleInterval(pmf, percentage=90):
    cdf = pmf.MakeCdf()
    prob = (1 - percentage / 100.0) / 2
    interval = cdf.Value(prob), cdf.Value(1 - prob)
    return interval

Response: Computes a credible interval for a given posterior distribution.


Instruction:
Implement a Python 3 function for
returning the probability that a value from pmf1 is less than a value from pmf2.

Response:
def PmfProbLess(pmf1, pmf2):
    """Probability that a value from pmf1 is less than a value from pmf2.

    Args:
        pmf1: Pmf object
        pmf2: Pmf object

    Returns:
        float probability
    """
    total = 0.0
    for v1, p1 in pmf1.Items():
        for v2, p2 in pmf2.Items():
            if v1 < v2:
                total += p1 * p2
    return total


Instruction:
Here you have a function in Python 3, explain what it does
def SampleSum(dists, n):
    pmf = MakePmfFromList(RandomSum(dists) for i in xrange(n))
    return pmf

Response: Draws a sample of sums from a list of distributions.


Instruction:
How would you explain what the following Python 3 function does
def EvalGaussianPdf(x, mu, sigma):
    return scipy.stats.norm.pdf(x, mu, sigma)

Response: Computes the unnormalized PDF of the normal distribution.


Instruction:
Write a Python 3 function for
making a PMF discrete approx to a Gaussian distribution.

Response:
def MakeGaussianPmf(mu, sigma, num_sigmas, n=201):
    """Makes a PMF discrete approx to a Gaussian distribution.

    mu: float mean
    sigma: float standard deviation
    num_sigmas: how many sigmas to extend in each direction
    n: number of values in the Pmf

    returns: normalized Pmf
    """
    pmf = Pmf()
    low = mu - num_sigmas * sigma
    high = mu + num_sigmas * sigma

    for x in numpy.linspace(low, high, n):
        p = EvalGaussianPdf(x, mu, sigma)
        pmf.Set(x, p)
    pmf.Normalize()
    return pmf


Instruction:
Here you have a function in Python 3, explain what it does
def EvalBinomialPmf(k, n, p):
    return scipy.stats.binom.pmf(k, n, p)

Response: Evaluates the binomial pmf.


Instruction:
Make a summary of the following Python 3 code
def EvalPoissonPmf(k, lam):
    # don't use the scipy function (yet).  for lam=0 it returns NaN;
    # should be 0.0
    # return scipy.stats.poisson.pmf(k, lam)

    return lam ** k * math.exp(-lam) / math.factorial(k)

Response: Computes the Poisson PMF.


Instruction:
Can you tell what is the following Python 3 function doing
def MakePoissonPmf(lam, high, step=1):
    pmf = Pmf()
    for k in xrange(0, high + 1, step):
        p = EvalPoissonPmf(k, lam)
        pmf.Set(k, p)
    pmf.Normalize()
    return pmf

Response: Makes a PMF discrete approx to a Poisson distribution.


Instruction:
Can you tell what is the following Python 3 function doing
def MakeExponentialPmf(lam, high, n=200):
    pmf = Pmf()
    for x in numpy.linspace(0, high, n):
        p = EvalExponentialPdf(x, lam)
        pmf.Set(x, p)
    pmf.Normalize()
    return pmf

Response: Makes a PMF discrete approx to an exponential distribution.


Instruction:
Here you have a function in Python 3, explain what it does
def GaussianCdfInverse(p, mu=0, sigma=1):
    x = ROOT2 * erfinv(2 * p - 1)
    return mu + x * sigma

Response: Evaluates the inverse CDF of the gaussian distribution.


Instruction:
Write a Python 3 script to
compute the log of the binomial coefficient.

Response:
def LogBinomialCoef(n, k):
    """Computes the log of the binomial coefficient.

    http://math.stackexchange.com/questions/64716/
    approximating-the-logarithm-of-the-binomial-coefficient

    n: number of trials
    k: number of successes

    Returns: float
    """
    return n * log(n) - k * log(k) - (n - k) * log(n - k)


Instruction:
Here you have a function in Python 3, explain what it does
def Lookup(self, x):
        return self._Bisect(x, self.xs, self.ys)

Response: Looks up x and returns the corresponding value of y.


Instruction:
How would you explain what the following Python 3 function does
def Reverse(self, y):
        return self._Bisect(y, self.ys, self.xs)

Response: Looks up y and returns the corresponding value of x.


Instruction:
Implement a Python 3 function for
initializing with a map from value to probability.

Response:
def InitMapping(self, values):
        """Initializes with a map from value to probability.

        values: map from value to probability
        """
        for value, prob in values.iteritems():
            self.Set(value, prob)


Instruction:
Create a Python 3 function for
initializing with a Pmf.

Response:
def InitPmf(self, values):
        """Initializes with a Pmf.

        values: Pmf object
        """
        for value, prob in values.Items():
            self.Set(value, prob)