Skip to content

# `` patch_serializer_example

Functions:

- `main` –

## `` main

    main()

Source code in `examples/patch_serializer_example.py`

     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60

    def main():
        # example for adding a quad
        ds = Dataset()
        g = Graph(identifier=URIRef("http://graph-a"))
        ds.add_graph(g)
        triple = (URIRef("http://subj-a"), URIRef("http://pred-a"), Literal("obj-a"))
        ds.get_context(g.identifier).add(triple)
        result = ds.serialize(format="patch", operation="add")
        print("Add Quad Patch:")
        print(result)

        # alternate example for adding a quad
        ds = Dataset()
        quad = (
            URIRef("http://subj-a"),
            URIRef("http://pred-a"),
            Literal("obj-a"),
            Graph(identifier=URIRef("http://graph-a")),
        )
        ds.add(quad)
        result = ds.serialize(format="patch", operation="add")
        print("Add Quad Patch:")
        print(result)

        # example for adding a triple
        ds = Dataset()
        ds.add(triple)
        result = ds.serialize(format="patch", operation="add")
        print("\nAdd Triple Patch:")
        print(result)

        # Example for diff quads
        quad_1 = (
            URIRef("http://subj-a"),
            URIRef("http://pred-a"),
            Literal("obj-a"),
            Graph(identifier=URIRef("http://graph-a")),
        )
        quad_2 = (
            URIRef("http://subj-b"),
            URIRef("http://pred-b"),
            Literal("obj-b"),
            Graph(identifier=URIRef("http://graph-b")),
        )
        quad_3 = (
            URIRef("http://subj-c"),
            URIRef("http://pred-c"),
            Literal("obj-c"),
            Graph(identifier=URIRef("http://graph-c")),
        )
        ds1 = Dataset()
        ds2 = Dataset()
        ds1.addN([quad_1, quad_2])
        ds2.addN([quad_2, quad_3])
        result = ds1.serialize(format="patch", target=ds2)
        print("Diff Quad Patch:")
        print(result)

        show_docstring_attributes: false
        show_docstring_functions: false
        show_docstring_modules: false
        show_docstring_classes: false
        show_signature: false
        show_signature_annotations: false
        show_signature_type_parameters: false
        show_docstring_other_parameters: false
        show_docstring_parameters: false
        show_docstring_raises: false
        show_docstring_receives: false
        show_docstring_returns: false
        summary: false
        show_if_no_docstring: false

    from rdflib import Dataset, Graph, Literal, URIRef

    def main():
        # example for adding a quad
        ds = Dataset()
        g = Graph(identifier=URIRef("http://graph-a"))
        ds.add_graph(g)
        triple = (URIRef("http://subj-a"), URIRef("http://pred-a"), Literal("obj-a"))
        ds.get_context(g.identifier).add(triple)
        result = ds.serialize(format="patch", operation="add")
        print("Add Quad Patch:")
        print(result)

        # alternate example for adding a quad
        ds = Dataset()
        quad = (
            URIRef("http://subj-a"),
            URIRef("http://pred-a"),
            Literal("obj-a"),
            Graph(identifier=URIRef("http://graph-a")),
        )
        ds.add(quad)
        result = ds.serialize(format="patch", operation="add")
        print("Add Quad Patch:")
        print(result)

        # example for adding a triple
        ds = Dataset()
        ds.add(triple)
        result = ds.serialize(format="patch", operation="add")
        print("\nAdd Triple Patch:")
        print(result)

        # Example for diff quads
        quad_1 = (
            URIRef("http://subj-a"),
            URIRef("http://pred-a"),
            Literal("obj-a"),
            Graph(identifier=URIRef("http://graph-a")),
        )
        quad_2 = (
            URIRef("http://subj-b"),
            URIRef("http://pred-b"),
            Literal("obj-b"),
            Graph(identifier=URIRef("http://graph-b")),
        )
        quad_3 = (
            URIRef("http://subj-c"),
            URIRef("http://pred-c"),
            Literal("obj-c"),
            Graph(identifier=URIRef("http://graph-c")),
        )
        ds1 = Dataset()
        ds2 = Dataset()
        ds1.addN([quad_1, quad_2])
        ds2.addN([quad_2, quad_3])
        result = ds1.serialize(format="patch", target=ds2)
        print("Diff Quad Patch:")
        print(result)

    if __name__ == "__main__":
        main()