| 1 |
# Work with PROV-N |
| 2 |
|
| 3 |
```{important} |
| 4 |
**PROV-N is write-only.** `prov` can produce [PROV-N](https://www.w3.org/TR/prov-n/) text, |
| 5 |
but there is no parser: deserializing PROV-N raises `NotImplementedError`. If you need to |
| 6 |
read a document back, save it in PROV-JSON, PROV-XML, or PROV-O/RDF instead. |
| 7 |
``` |
| 8 |
|
| 9 |
PROV-N needs no extra dependency. |
| 10 |
|
| 11 |
## Get the PROV-N text directly |
| 12 |
|
| 13 |
{py:meth}`~prov.model.ProvBundle.get_provn` returns the notation as a string without going |
| 14 |
through the serializer registry at all — this is the simplest way to print or inspect it: |
| 15 |
|
| 16 |
```python |
| 17 |
import prov.model as pm |
| 18 |
|
| 19 |
document = pm.ProvDocument() |
| 20 |
document.set_default_namespace("http://example.org/") |
| 21 |
e = document.entity("e1") |
| 22 |
a = document.activity("a1") |
| 23 |
document.wasGeneratedBy(e, a) |
| 24 |
|
| 25 |
print(document.get_provn()) |
| 26 |
``` |
| 27 |
|
| 28 |
## Serialize to a file |
| 29 |
|
| 30 |
`format="provn"` goes through the same {py:meth}`~prov.model.ProvDocument.serialize` API |
| 31 |
as the other formats, for consistency with tooling that dispatches on `format`: |
| 32 |
|
| 33 |
```python |
| 34 |
document.serialize("document.provn", format="provn") |
| 35 |
``` |
| 36 |
|
| 37 |
## Serialize to a string |
| 38 |
|
| 39 |
```python |
| 40 |
provn_str = document.serialize(format="provn") |
| 41 |
assert provn_str == document.get_provn() |
| 42 |
``` |
| 43 |
|
| 44 |
## Deserializing raises `NotImplementedError` |
| 45 |
|
| 46 |
There is no PROV-N reader, in the library or via `prov.read()`'s auto-detection: |
| 47 |
|
| 48 |
```python |
| 49 |
try: |
| 50 |
pm.ProvDocument.deserialize("document.provn", format="provn") |
| 51 |
except NotImplementedError: |
| 52 |
print("PROV-N has no deserializer") |
| 53 |
``` |
| 54 |
|
| 55 |
If your workflow needs a round trip, keep a PROV-JSON (or PROV-XML/RDF) copy alongside any |
| 56 |
PROV-N output — see {doc}`provjson`. |