Skip to content

# `` hext

HextuplesSerializer RDF graph serializer for RDFLib. See https://github.com/ontola/hextuples for details about the format.

Classes:

- `HextuplesSerializer` –

  Serializes RDF graphs to NTriples format.

## `` __all__ `module-attribute`

    __all__ = ['HextuplesSerializer']

## `` HextuplesSerializer

    HextuplesSerializer(store: Union[Graph, Dataset, ConjunctiveGraph])

Bases: `Serializer`

Serializes RDF graphs to NTriples format.

Methods:

- `__new__` –
- `serialize` –

Attributes:

- `contexts` (`List[Union[Graph, IdentifiedNode]]`) –
- `default_context` (`Optional[Union[Graph, IdentifiedNode]]`) –
- `dumps` (`Callable`) –
- `graph_type` (`Union[Type[Graph], Type[Dataset], Type[ConjunctiveGraph]]`) –

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

    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73

    def __init__(self, store: Union[Graph, Dataset, ConjunctiveGraph]):
        self.default_context: Optional[Union[Graph, IdentifiedNode]]
        self.graph_type: Union[Type[Graph], Type[Dataset], Type[ConjunctiveGraph]]
        if isinstance(store, (Dataset, ConjunctiveGraph)):
            self.graph_type = (
                Dataset if isinstance(store, Dataset) else ConjunctiveGraph
            )
            self.contexts = list(store.contexts())
            if store.default_context:
                self.default_context = store.default_context
                self.contexts.append(store.default_context)
            else:
                self.default_context = None
        else:
            self.graph_type = Graph
            self.contexts = [store]
            self.default_context = None

        Serializer.__init__(self, store)

### `` contexts `instance-attribute`

    contexts: List[Union[Graph, IdentifiedNode]]

### `` default_context `instance-attribute`

    default_context: Optional[Union[Graph, IdentifiedNode]]

### `` dumps `instance-attribute`

    dumps: Callable

### `` graph_type `instance-attribute`

    graph_type: Union[Type[Graph], Type[Dataset], Type[ConjunctiveGraph]]

### `` __new__

    __new__(store: Union[Graph, Dataset, ConjunctiveGraph])

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

    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53

    def __new__(cls, store: Union[Graph, Dataset, ConjunctiveGraph]):
        if _HAS_ORJSON:
            cls.str_local_id: Union[str, Any] = orjson.Fragment(b'"localId"')
            cls.str_global_id: Union[str, Any] = orjson.Fragment(b'"globalId"')
            cls.empty: Union[str, Any] = orjson.Fragment(b'""')
            cls.lang_str: Union[str, Any] = orjson.Fragment(
                b'"' + RDF.langString.encode("utf-8") + b'"'
            )
            cls.xsd_string: Union[str, Any] = orjson.Fragment(
                b'"' + XSD.string.encode("utf-8") + b'"'
            )
        else:
            cls.str_local_id = "localId"
            cls.str_global_id = "globalId"
            cls.empty = ""
            cls.lang_str = f"{RDF.langString}"
            cls.xsd_string = f"{XSD.string}"
        return super(cls, cls).__new__(cls)

### `` serialize

    serialize(stream: IO[bytes], base: Optional[str] = None, encoding: Optional[str] = 'utf-8', **kwargs: Any) -> None

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

     75
     76
     77
     78
     79
     80
     81
     82
     83
     84
     85
     86
     87
     88
     89
     90
     91
     92
     93
     94
     95
     96
     97
     98
     99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119

    def serialize(
        self,
        stream: IO[bytes],
        base: Optional[str] = None,
        encoding: Optional[str] = "utf-8",
        **kwargs: Any,
    ) -> None:
        if base is not None:
            warnings.warn(
                "base has no meaning for Hextuples serialization. "
                "I will ignore this value"
            )

        if encoding not in [None, "utf-8"]:
            warnings.warn(
                f"Hextuples files are always utf-8 encoded. "
                f"I was passed: {encoding}, "
                "but I'm still going to use utf-8 anyway!"
            )

        if self.store.formula_aware is True:
            raise Exception(
                "Hextuple serialization can't (yet) handle formula-aware stores"
            )
        context: Union[Graph, IdentifiedNode]
        context_str: Union[bytes, str]
        for context in self.contexts:
            for triple in context:
                # Generate context string just once, because it doesn't change
                # for every triple in this context
                context_str = cast(
                    Union[str, bytes],
                    (
                        self.empty
                        if self.graph_type is Graph
                        else (
                            orjson.Fragment('"' + self._context_str(context) + '"')
                            if _HAS_ORJSON
                            else self._context_str(context)
                        )
                    ),
                )
                hl = self._hex_line(triple, context_str)
                if hl is not None:
                    stream.write(hl if _HAS_ORJSON else hl.encode())