Skip to content

dict2graph.Transformer

Source code in dict2graph/transformers/_base.py
class Transformer:
    class NodeTransformerMatcher:
        def _set_node_matcher(
            self,
            label_match: Union[str, List[str], AnyLabel],
            has_one_label_of: List[str] = None,
            has_none_label_of: List[str] = None,
        ):
            if isinstance(label_match, str):
                label_match = [label_match]
            self.label_match: Union[List[str], AnyLabel] = label_match

            self.has_one_label_of = has_one_label_of
            self.has_none_label_of = has_none_label_of

        def _match(self, node: Node) -> bool:
            if node.deleted:
                return False
            if self.has_none_label_of is not None and not set(
                self.has_none_label_of
            ).isdisjoint(set(node.labels)):
                return False
            if self.has_one_label_of is not None and set(
                self.has_one_label_of
            ).intersection(set(node.labels)):
                return True
            if self.label_match == AnyLabel or (
                self.label_match is not None
                and set(self.label_match).issubset(set(node.labels))
            ):
                return True

            return False

        def do(
            self, transform: Union[_NodeTransformerBase, List[_NodeTransformerBase]]
        ) -> Union[_NodeTransformerBase, List[_NodeTransformerBase]]:
            if isinstance(transform, list):
                transformers = []
                for trans in transform:
                    transformers.append(self.do(trans))
                return transformers
            else:
                transform._set_matcher(self)
            return transform

    class RelTransformerMatcher:
        def _set_rel_matcher(
            self,
            relation_type_match: Union[str, List[str], AnyRelation],
            relation_type_is_not_in: List[str],
        ):
            if isinstance(relation_type_match, str):
                self.relation_type_match = [relation_type_match]
            else:
                self.relation_type_match = relation_type_match
            self.relation_type_match = relation_type_match

            if relation_type_is_not_in:
                self.relation_type_is_not_in = relation_type_is_not_in
            else:
                self.relation_type_is_not_in = []

        def _match(self, rel: Relation) -> bool:
            if (
                self.relation_type_match in [None, AnyRelation]
                or rel.relation_type in self.relation_type_match
            ) and rel.relation_type not in self.relation_type_is_not_in:
                return True
            return False

        def do(
            self, transform: _RelationTransformerBase
        ) -> Union[_NodeTransformerBase, List[_NodeTransformerBase]]:
            if isinstance(transform, list):
                transformers = []
                for trans in transform:
                    transformers.append(self.do(trans))
                return transformers
            else:
                transform._set_matcher(self)
            return transform

    @classmethod
    def match_nodes(
        cls,
        has_labels: Union[str, List[str], AnyLabel] = AnyLabel,
        has_one_label_of: List[str] = None,
        has_none_label_of: List[str] = None,
    ) -> NodeTransformerMatcher:
        """Match nodes to apply tranformers

        Args:
            has_labels (Union[str, List[str], AnyLabel], optional): _description_. Defaults to AnyLabel.
            has_one_label_of (List[str], optional): _description_. Defaults to None.
            has_none_label_of (List[str], optional): _description_. Defaults to None.

        Returns:
            NodeTransformerMatcher: _description_
        """
        tm = Transformer.NodeTransformerMatcher()
        if has_one_label_of or has_none_label_of and has_labels == AnyLabel:
            has_labels = None
        tm._set_node_matcher(
            label_match=has_labels,
            has_one_label_of=has_one_label_of,
            has_none_label_of=has_none_label_of,
        )
        return tm

    @classmethod
    def match_rels(
        cls,
        relation_type: Union[str, List[str], AnyRelation] = AnyRelation,
        relation_type_is_not_in: List[str] = None,
    ) -> RelTransformerMatcher:
        """Match relationships to apply tranformers

        Args:
            relation_type (Union[str, List[str], AnyRelation], optional): A relation type as string or mulitple relation types as list of string.. Defaults to AnyRelation.
            relation_type_is_not_in (List[str], optional): _description_. Defaults to None.

        Returns:
            RelTransformerMatcher: _description_
        """
        tm = Transformer.RelTransformerMatcher()
        tm._set_rel_matcher(
            relation_type_match=relation_type,
            relation_type_is_not_in=relation_type_is_not_in,
        )
        return tm

match_nodes(has_labels=AnyLabel, has_one_label_of=None, has_none_label_of=None) classmethod

Match nodes to apply tranformers

Parameters:

Name Type Description Default
has_labels Union[str, List[str], AnyLabel]

description. Defaults to AnyLabel.

AnyLabel
has_one_label_of List[str]

description. Defaults to None.

None
has_none_label_of List[str]

description. Defaults to None.

None

Returns:

Name Type Description
NodeTransformerMatcher NodeTransformerMatcher

description

Source code in dict2graph/transformers/_base.py
@classmethod
def match_nodes(
    cls,
    has_labels: Union[str, List[str], AnyLabel] = AnyLabel,
    has_one_label_of: List[str] = None,
    has_none_label_of: List[str] = None,
) -> NodeTransformerMatcher:
    """Match nodes to apply tranformers

    Args:
        has_labels (Union[str, List[str], AnyLabel], optional): _description_. Defaults to AnyLabel.
        has_one_label_of (List[str], optional): _description_. Defaults to None.
        has_none_label_of (List[str], optional): _description_. Defaults to None.

    Returns:
        NodeTransformerMatcher: _description_
    """
    tm = Transformer.NodeTransformerMatcher()
    if has_one_label_of or has_none_label_of and has_labels == AnyLabel:
        has_labels = None
    tm._set_node_matcher(
        label_match=has_labels,
        has_one_label_of=has_one_label_of,
        has_none_label_of=has_none_label_of,
    )
    return tm

match_rels(relation_type=AnyRelation, relation_type_is_not_in=None) classmethod

Match relationships to apply tranformers

Parameters:

Name Type Description Default
relation_type Union[str, List[str], AnyRelation]

A relation type as string or mulitple relation types as list of string.. Defaults to AnyRelation.

AnyRelation
relation_type_is_not_in List[str]

description. Defaults to None.

None

Returns:

Name Type Description
RelTransformerMatcher RelTransformerMatcher

description

Source code in dict2graph/transformers/_base.py
@classmethod
def match_rels(
    cls,
    relation_type: Union[str, List[str], AnyRelation] = AnyRelation,
    relation_type_is_not_in: List[str] = None,
) -> RelTransformerMatcher:
    """Match relationships to apply tranformers

    Args:
        relation_type (Union[str, List[str], AnyRelation], optional): A relation type as string or mulitple relation types as list of string.. Defaults to AnyRelation.
        relation_type_is_not_in (List[str], optional): _description_. Defaults to None.

    Returns:
        RelTransformerMatcher: _description_
    """
    tm = Transformer.RelTransformerMatcher()
    tm._set_rel_matcher(
        relation_type_match=relation_type,
        relation_type_is_not_in=relation_type_is_not_in,
    )
    return tm