Skip to content

# `` n3

Notation 3 (N3) RDF graph serializer for RDFLib.

Classes:

- `N3Serializer` –

  Notation 3 (N3) RDF graph serializer.

## `` SWAP_LOG `module-attribute`

    SWAP_LOG = Namespace('http://www.w3.org/2000/10/swap/log#')

## `` __all__ `module-attribute`

    __all__ = ['N3Serializer']

## `` N3Serializer

    N3Serializer(store: Graph, parent=None)

Bases: `TurtleSerializer`

Notation 3 (N3) RDF graph serializer.

Methods:

- `endDocument` –
- `getQName` –
- `get_pname` –
- `indent` –
- `p_clause` –
- `path` –
- `preprocessTriple` –
- `reset` –
- `s_clause` –
- `statement` –

Attributes:

- `parent` –
- `short_name` –

Source code in `rdflib/plugins/serializers/n3.py`

    21
    22
    23
    24

    def __init__(self, store: Graph, parent=None):
        super(N3Serializer, self).__init__(store)
        self.keywords.update({OWL.sameAs: "=", SWAP_LOG.implies: "=>"})
        self.parent = parent

### `` parent `instance-attribute`

    parent = parent

### `` short_name `class-attribute` `instance-attribute`

    short_name = 'n3'

### `` endDocument

    endDocument()

Source code in `rdflib/plugins/serializers/n3.py`

    30
    31
    32

    def endDocument(self):  # noqa: N802
        if not self.parent:
            super(N3Serializer, self).endDocument()

### `` getQName

    getQName(uri, gen_prefix=True)

Source code in `rdflib/plugins/serializers/n3.py`

    60
    61
    62
    63
    64
    65
    66

    def getQName(self, uri, gen_prefix=True):  # noqa: N802
        warnings.warn(
            "N3Serializer.getQName is deprecated, use N3Serializer.get_pname instead.",
            DeprecationWarning,
            stacklevel=2,
        )
        return self.get_pname(uri, gen_prefix)

### `` get_pname

    get_pname(uri, gen_prefix=True)

Source code in `rdflib/plugins/serializers/n3.py`

    52
    53
    54
    55
    56
    57
    58

    def get_pname(self, uri, gen_prefix=True):
        qname = None
        if self.parent is not None:
            qname = self.parent.get_pname(uri, gen_prefix)
        if qname is None:
            qname = super(N3Serializer, self).get_pname(uri, gen_prefix)
        return qname

### `` indent

    indent(modifier=0)

Source code in `rdflib/plugins/serializers/n3.py`

    34
    35
    36
    37
    38

    def indent(self, modifier=0):
        indent = super(N3Serializer, self).indent(modifier)
        if self.parent is not None:
            indent += self.parent.indent()  # modifier)
        return indent

### `` p_clause

    p_clause(node, position)

Source code in `rdflib/plugins/serializers/n3.py`

     89
     90
     91
     92
     93
     94
     95
     96
     97
     98
     99
    100
    101
    102
    103

    def p_clause(self, node, position):
        if isinstance(node, Graph):
            self.subjectDone(node)
            if position is OBJECT:
                self.write(" ")
            self.write("{")
            self.depth += 1
            serializer = N3Serializer(node, parent=self)
            # type error: Argument 1 to "serialize" of "TurtleSerializer" has incompatible type "Optional[IO[bytes]]"; expected "IO[bytes]"
            serializer.serialize(self.stream)  # type: ignore[arg-type]
            self.depth -= 1
            self.write(self.indent() + "}")
            return True
        else:
            return False

### `` path

    path(node, position, newline=False)

Source code in `rdflib/plugins/serializers/n3.py`

    75
    76
    77

    def path(self, node, position, newline=False):
        if not self.p_clause(node, position):
            super(N3Serializer, self).path(node, position, newline)

### `` preprocessTriple

    preprocessTriple(triple)

Source code in `rdflib/plugins/serializers/n3.py`

    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50

    def preprocessTriple(self, triple):  # noqa: N802
        super(N3Serializer, self).preprocessTriple(triple)
        if isinstance(triple[0], Graph):
            for t in triple[0]:
                self.preprocessTriple(t)
        if isinstance(triple[1], Graph):
            for t in triple[1]:
                self.preprocessTriple(t)
        if isinstance(triple[2], Graph):
            for t in triple[2]:
                self.preprocessTriple(t)

### `` reset

    reset()

Source code in `rdflib/plugins/serializers/n3.py`

    26
    27
    28

    def reset(self):
        super(N3Serializer, self).reset()
        self._stores = {}

### `` s_clause

    s_clause(subject)

Source code in `rdflib/plugins/serializers/n3.py`

    79
    80
    81
    82
    83
    84
    85
    86
    87

    def s_clause(self, subject):
        if isinstance(subject, Graph):
            self.write("\n" + self.indent())
            self.p_clause(subject, SUBJECT)
            self.predicateList(subject)
            self.write(" .")
            return True
        else:
            return False

### `` statement

    statement(subject)

Source code in `rdflib/plugins/serializers/n3.py`

    68
    69
    70
    71
    72
    73

    def statement(self, subject):
        self.subjectDone(subject)
        properties = self.buildPredicateHash(subject)
        if len(properties) == 0:
            return False
        return self.s_clause(subject) or super(N3Serializer, self).statement(subject)