#1 Compile class inheriting from `Rule` into a `Rule` trait implementation

باز‌کردن
5 سال پیش باز شده توسط unidan · 0 دیدگاه

Example:

class EpisodeSingleDigitValidator(Rule):
    """
    Remove single digit episode when inside a group that doesn't own title.
    """
    dependency = [TitleFromPosition]

    consequence = RemoveMatch

    def when(self, matches, context):
        ret = []
        for episode in matches.named('episode', lambda match: len(match.initiator) == 1):
            group = matches.markers.at_match(episode, lambda marker: marker.name == 'group', index=0)
            if group:
                if not matches.range(*group.span, predicate=lambda match: match.name == 'title'):
                    ret.append(episode)
        return ret

Should implement something like

trait Rule {
    fn dependencies(&self) -> Vec<Dependency>;
    fn consequence(&self) -> Consequence;
    fn when(&self, matches: Matches, context: Context) -> Vec<Marker>;
}
Example: ``` class EpisodeSingleDigitValidator(Rule): """ Remove single digit episode when inside a group that doesn't own title. """ dependency = [TitleFromPosition] consequence = RemoveMatch def when(self, matches, context): ret = [] for episode in matches.named('episode', lambda match: len(match.initiator) == 1): group = matches.markers.at_match(episode, lambda marker: marker.name == 'group', index=0) if group: if not matches.range(*group.span, predicate=lambda match: match.name == 'title'): ret.append(episode) return ret ``` Should implement something like ``` trait Rule { fn dependencies(&self) -> Vec<Dependency>; fn consequence(&self) -> Consequence; fn when(&self, matches: Matches, context: Context) -> Vec<Marker>; } ```
برای پیوستن به گفتگو، وارد شودید.
بدون برچسب
بدون نقطه عطف
بدون مسئول رسیدگی
1 مشارکت کننده
درحال بارگذاری...
لغو
ذخيره
هنوز محتوایی ایجاد نشده.