一组规则,通过应用组中的每个规则来检查包,并返回一个规范化的总分数和所有检查的组合消息。
属性
| 名称 |
类型 |
描述 |
rules |
list[ScoreRule]
|
|
方法
| 名称 |
描述 |
check |
针对此组中的每个评分规则检查给定包,并返回一个 CheckResult 实例,其中包含规范化的总分数和组合消息。
|
源代码在 searchv2/rules.py 中
| class ScoreRuleGroup(ScoreRule):
"""
A group of rules, which checks a package by applying each rule in the group,
and returns a normalized total score and a combined message from all checks.
Attributes:
rules: A list of ScoreRule objects in this group.
Methods:
check: Checks the given package against each scoring rule in this group, and returns a CheckResult instance
with the normalized total score and a combined message.
"""
rules: list[ScoreRule]
def check(self, package: Package) -> CheckResult:
"""
Checks the given package against each scoring rule in this group, and returns a CheckResult instance
with the normalized total score and a combined message.
The total score is calculated by summing up the scores from each rule.
It is then normalized by dividing it by the sum of maximum scores of all the rules in the group, and multiplying
by the maximum score of this group.
If the sum of the maximum scores of all the rules in the group is zero, the normalized total score is set to zero.
Args:
package: The package to check.
Returns:
A CheckResult instance with the normalized total score and a combined message from all checks.
"""
results = [rule.check(package=package) for rule in self.rules]
total_score = sum(result.score for result in results)
max_possible_score = sum(rule.max_score for rule in self.rules)
# Normalize the total score to the max score of this group.
normalized_score = (
(total_score / max_possible_score) * self.max_score
if max_possible_score > 0
else 0
)
# Combine all the messages from the checks.
messages = [result.message for result in results]
return CheckResult(score=int(normalized_score), message=" ".join(messages))
|
Config
ScoreRule 的配置类。在赋值时启用验证。
源代码在 searchv2/rules.py 中
| class Config:
"""
Config class for ScoreRule.
Enables validation on assignment.
"""
validate_assignment = True
|
validate_assignment class-attribute instance-attribute
validate_assignment = True
description instance-attribute
documentation_url class-attribute instance-attribute
max_score instance-attribute