<commit_before>from .relationship import RelationshipManager, ZeroOrMore


class ZeroOrOne(RelationshipManager):
    description = "zero or one relationship"

    def single(self):
        nodes = super(ZeroOrOne, self).all()
        if not nodes:
            return
        if len(nodes) == 1:
            return nodes[0]
        if len(nodes) > 1:
            raise CardinalityViolation(self, len(nodes))

    def all(self):
        node = self.single()
        return [node] if node else []

    def connect(self, obj):
        if self.origin.__node__.has_relationship(self.direction, self.relation_type):
            raise AttemptedCardinalityViolation("Node already has {0} can't connect more".format(self))
        else:
            return super(ZeroOrOne, self).connect(obj)


class OneOrMore(RelationshipManager):
    description = "one or more relationships"

    def single(self):
        nodes = super(OneOrMore, self).all()
        if nodes:
            return nodes[0]
        raise CardinalityViolation(self, 'none')

    def all(self):
        nodes = super(OneOrMore, self).all()
        if nodes:
            return nodes
        raise CardinalityViolation(self, 'none')

    def disconnect(self, obj):
        if len(self.origin.__node__.get_related_nodes(self.direction, self.relation_type)) < 2:
            raise AttemptedCardinalityViolation("One or more expected")
        return super(OneOrMore, self).disconnect(obj)


class One(RelationshipManager):
    description = "one relationship"

    def single(self):
        nodes = super(One, self).all()
        if nodes:
            if len(nodes) == 1:
                return nodes[0]
            else:
                raise CardinalityViolation(self, len(nodes))<commit_msg>Use superclasses 'len'<commit_after>from .relationship import RelationshipManager, ZeroOrMore


class ZeroOrOne(RelationshipManager):
    description = "zero or one relationship"

    def single(self):
        nodes = super(ZeroOrOne, self).all()
        if not nodes:
            return
        if len(nodes) == 1:
            return nodes[0]
        if len(nodes) > 1:
            raise CardinalityViolation(self, len(nodes))

    def all(self):
        node = self.single()
        return [node] if node else []

    def connect(self, obj):
        if self.origin.__node__.has_relationship(self.direction, self.relation_type):
            raise AttemptedCardinalityViolation("Node already has {0} can't connect more".format(self))
        else:
            return super(ZeroOrOne, self).connect(obj)


class OneOrMore(RelationshipManager):
    description = "one or more relationships"

    def single(self):
        nodes = super(OneOrMore, self).all()
        if nodes:
            return nodes[0]
        raise CardinalityViolation(self, 'none')

    def all(self):
        nodes = super(OneOrMore, self).all()
        if nodes:
            return nodes
        raise CardinalityViolation(self, 'none')

    def disconnect(self, obj):
        if super(OneOrMore, self).__len__() < 2:
            raise AttemptedCardinalityViolation("One or more expected")
        return super(OneOrMore, self).disconnect(obj)


class One(RelationshipManager):
    description = "one relationship"

    def single(self):
        nodes = super(One, self).all()
        if nodes:
            if len(nodes) == 1:
                return nodes[0]
            else:
                raise CardinalityViolation(self, len(nodes))
{'fraction_non_alphanumeric': 0.10069444444444445, 'fraction_numerical': 0.004629629629629629, 'mean_word_length': 2.4193867457962415, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '4703736', 'n_tokens_mistral': 986, 'n_tokens_neox': 943, 'n_words': 253}