Skip to content

tag_model.py

ofrak.model.tag_model

ResourceTag (type)

tag_specificity(cls)

Indicates how specific an abstraction this tag is.

Returns:

Type Description
int

The number of classes in the inheritance hierarchy between this class and Resource

Source code in ofrak/model/tag_model.py
@functools.lru_cache(None)
def tag_specificity(cls) -> int:
    """
    Indicates how specific an abstraction this tag is.
    :return: The number of classes in the inheritance hierarchy between this class and
    Resource
    """
    specificity = 0
    for base in cls.base_tags():
        specificity = max(specificity, base.tag_specificity())

    return specificity + 1

tag_classes(cls)

Returns:

Type Description
Set[ResourceTag]

Set of parent classes (including itself) that are a subclass of a _ResourceTag but not the _ResourceTag class.

Source code in ofrak/model/tag_model.py
@functools.lru_cache(None)
def tag_classes(cls) -> Set["ResourceTag"]:
    """
    :return: Set of parent classes (including itself) that are a subclass of a _ResourceTag but
    not the _ResourceTag class.
    """
    parents = set()
    parents.add(cls)
    for base in cls.base_tags():
        parents.update(base.tag_classes())
    return parents

base_tags(cls)

Returns:

Type Description
Set[ResourceTag]

All _ResourceTags which this tag inherits from directly (does not traverse all ancestors)

Source code in ofrak/model/tag_model.py
@functools.lru_cache(None)
def base_tags(cls) -> Set["ResourceTag"]:
    """
    :return: All _ResourceTags which this tag inherits from directly (does not traverse all
    ancestors)
    """
    base_tags = set()
    for base in cls.__bases__:
        if base is not cls and isinstance(base, ResourceTag) and base.tag_specificity() > 0:
            base_tags.add(base)
    return base_tags

sort_tags_into_tiers(tags) staticmethod

Sort the given tags by specificity, and group all the ties together.

Parameters:

Name Type Description Default
tags Iterable[ResourceTag]

Tags to sort and group

required

Returns:

Type Description
Tuple[Tuple[ResourceTag, ...], ...]

Tuple of groups of tags with the same specificity, sorting all of these by the specificity value each group represents from least to greatest.

Source code in ofrak/model/tag_model.py
@staticmethod
def sort_tags_into_tiers(
    tags: "Iterable[ResourceTag]",
) -> "Tuple[Tuple[ResourceTag, ...], ...]":
    """
    Sort the given tags by specificity, and group all the ties together.

    :param tags: Tags to sort and group

    :return: Tuple of groups of tags with the same specificity, sorting all of these by the
    specificity value each group represents from least to greatest.
    """
    level_dict = defaultdict(list)
    for t in tags:
        level_dict[t.tag_specificity()].append(t)

    level_list = [tuple(level_dict[level]) for level in sorted(level_dict.keys())]
    return tuple(level_list)