<commit_before>import collections
from dataclasses import dataclass
from typing import Dict, Any

from siebenapp.domain import Graph, Command, with_key


@dataclass(frozen=True)
class FilterBy(Command):
    """Activate or deactivate filtering"""

    pattern: str


class FilterView(Graph):
    def __init__(self, goaltree: Graph):
        super().__init__(goaltree)
        self.pattern = ""

    def events(self) -> collections.deque:
        return self.goaltree.events()

    def verify(self) -> bool:
        return self.goaltree.verify()

    def settings(self, key: str) -> int:
        return self.goaltree.settings(key)

    def accept_FilterBy(self, event: FilterBy):
        self.pattern = event.pattern.lower()

    @with_key("name")
    def q(self, keys: str = "name") -> Dict[int, Any]:
        if skip_select := "select" not in keys:
            keys = ",".join([keys, "select"])
        unfiltered = self.goaltree.q(keys)
        result = self._filter(unfiltered, keys) if self.pattern else unfiltered
        if skip_select:
            for v in result.values():
                v.pop("select")
        return result

    def _filter(self, unfiltered, keys):
        accepted_nodes = [
            goal_id
            for goal_id, attrs in unfiltered.items()
            if self.pattern in attrs["name"].lower()
            or attrs.get("select", None) is not None
        ]
        filtered: Dict[int, Any] = {}
        for goal_id, attrs in unfiltered.items():
            if goal_id in accepted_nodes:
                if "edge" in keys:
                    attrs["edge"] = [e for e in attrs["edge"] if e[0] in accepted_nodes]
                filtered[goal_id] = attrs
        return filtered<commit_msg>Use @with_key for select in FilterView (4/n)<commit_after>import collections
from dataclasses import dataclass
from typing import Dict, Any

from siebenapp.domain import Graph, Command, with_key


@dataclass(frozen=True)
class FilterBy(Command):
    """Activate or deactivate filtering"""

    pattern: str


class FilterView(Graph):
    def __init__(self, goaltree: Graph):
        super().__init__(goaltree)
        self.pattern = ""

    def events(self) -> collections.deque:
        return self.goaltree.events()

    def verify(self) -> bool:
        return self.goaltree.verify()

    def settings(self, key: str) -> int:
        return self.goaltree.settings(key)

    def accept_FilterBy(self, event: FilterBy):
        self.pattern = event.pattern.lower()

    @with_key("name")
    @with_key("select")
    def q(self, keys: str = "name") -> Dict[int, Any]:
        unfiltered = self.goaltree.q(keys)
        return self._filter(unfiltered, keys) if self.pattern else unfiltered

    def _filter(self, unfiltered, keys):
        accepted_nodes = [
            goal_id
            for goal_id, attrs in unfiltered.items()
            if self.pattern in attrs["name"].lower()
            or attrs.get("select", None) is not None
        ]
        filtered: Dict[int, Any] = {}
        for goal_id, attrs in unfiltered.items():
            if goal_id in accepted_nodes:
                if "edge" in keys:
                    attrs["edge"] = [e for e in attrs["edge"] if e[0] in accepted_nodes]
                filtered[goal_id] = attrs
        return filtered
{'fraction_non_alphanumeric': 0.11158406810580723, 'fraction_numerical': 0.0009121313469139556, 'mean_word_length': 2.395252837977296, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6301645', 'n_tokens_mistral': 999, 'n_tokens_neox': 966, 'n_words': 274}