Small attempt to make most linkml functionalities work with the generated pydantic models or dataclasses. In the future linkml might properly handle things and then this repo will become obsolete.
Project description
LinkML Dumper Utils
This is a small helper package to get the dumping with linkml models more aligned with the practices of pydantic and Python dataclasses. Namely, using model_dump_* and as* methods. Moreover, some tooling to directly add linkml schema objects to your classes is included to be able to produce different serializations out of the box.
At the moment the serialization is supported for both JsonLD and RDF. However, the JSON LD implementation is not actually using the linkml runtime dumpers, but replicates some of its functionality but in a more convenient way.
Installation
pip install linkml-dumper-utils
Example usage
from pydantic import BaseModel
from linkml_dumper_utils.pydantic import PydanticJsonldMixin
class MyClass(BaseModel, PydanticJsonldMixin, context_file_path='myContext.json'):
name: str
obj = MyClass(name='Bob')
jsonld = obj.model_dump_jsonld()
Considering the context of myContext.json looks like the following
{
"foaf": "http://xmlns.com/foaf/0.1/",
"dct": "http://purl.org/dc/terms/",
"Person": "foaf:Person",
"name": "dct:title"
}
The jsonld object above would be a Python dict looking like this:
{
"@context": {
"foaf": "http://xmlns.com/foaf/0.1/",
"dct": "http://purl.org/dc/terms/",
"Person": "foaf:Person",
"name": "dct:title"
},
"@type": "Person",
"name": "Bob"
}
The context for a class can be specified in three ways:
- context_file_path
- context_string (also works as context URL - if double quoted)
- context (as a dict)
Next to JSON LD there is also the mixin for rdf data. It works exactly the same however, the arguments for class creation now include the following three options:
- schema_file_path
- schema_string (YAML or JSON as a string variable)
- schema_view (from linkml runtime)
Usage for pydantic
The following methods have been added to your pydantic classes depending on the used mixin:
- PydanticJsonldMixin
- model_dump_jsonld
- model_dump_jsonld_string
- PydanticRdflibMixin
- model_dump_rdflib_graph
- model_dump_rdf_string
There are several ways how this package can be used to interact with both existing pydantic models that can not be modified and your own pydantic models that you can simply update to reflect what you need.
Pydantic model using mixin via inheritance
from pydantic import BaseModel
from linkml_dumper_utils.pydantic import PydanticJsonldMixin
class MyClass(BaseModel, PydanticJsonldMixin, context_file_path='myContext.json'):
...
Pydantic model using decorator
from pydantic import BaseModel
from linkml_dumper_utils.pydantic import pydantic_rdflib_mixin
@pydantic_rdflib_mixin(schema_file_path='myModel.yaml')
class MyClass(BaseModel):
...
Pydantic with manual decoration
from pydantic import BaseModel
from linkml_dumper_utils.pydantic import pydantic_rdflib_mixin
class MyClass(BaseModel):
...
MyClass = pydantic_rdflib_mixin(schema_file_path='myModel.yaml')(MyClass)
Pydantic replacing third party models
Sometimes you would want to create a wrapper module to encapsulate certain classes from a third party module.
I recommend something like the following (though I have not explicity tested it).
Create a file wrapper.py to contain the modified class definitions so you can then use from wrapper import ClassA.
import third_party # use whatever module you want to modify
from linkml_dumper_utils.pydantic import pydantic_jsonld_mixin
for name in dir(third_party):
obj = getattr(third_party, name)
globals()[name] = pydantic_jsonld_mixin(context_file_path='myContext.json')(obj)
Of course, this is a little hacky but it might just do the trick depending on your context.
Usage for dataclasses
The behavior options are exactly the same for dataclasses.
However, the usage pattern for the actual dumpers is different in dataclasses. As they do not use member functions as dumpers but global functions that receive the object to be dumped as the first argument.
The available methods are:
- DataclassJsonldMixin
- as_json_string
- as_json_ld
- as_json_ld_string
- DataclassRdflibMixin
- as_rdflib_graph
- as_rdf_string
Example
from dataclasses import dataclass
from linkml_dumper_utils.dataclass import DataclassJsonldMixin, as_json_ld
@dataclass
class MyClass(DataclassJsonldMixin, context_file_path='my_context.json'):
x: int
test = MyClass(x=42)
data = as_json_ld(test)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file linkml_dumper_utils-1.0.0.tar.gz.
File metadata
- Download URL: linkml_dumper_utils-1.0.0.tar.gz
- Upload date:
- Size: 11.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9d32b7fad63654c2232d7316b058609ed62c28783890fdc236519c9e8612ac4b
|
|
| MD5 |
762611264971cf69fee8b65c12cda86b
|
|
| BLAKE2b-256 |
f55253c441d2d7fddf3bdfd1679508867bdcec26521f824cb34ebff493ec3896
|
Provenance
The following attestation bundles were made for linkml_dumper_utils-1.0.0.tar.gz:
Publisher:
python-publish.yml on Cpprentice/linkml-dumper-utils
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
linkml_dumper_utils-1.0.0.tar.gz -
Subject digest:
9d32b7fad63654c2232d7316b058609ed62c28783890fdc236519c9e8612ac4b - Sigstore transparency entry: 409428757
- Sigstore integration time:
-
Permalink:
Cpprentice/linkml-dumper-utils@d5e9dd7ee5129ba096876c52e472db31b62b54a1 -
Branch / Tag:
refs/tags/v1.0.0 - Owner: https://github.com/Cpprentice
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@d5e9dd7ee5129ba096876c52e472db31b62b54a1 -
Trigger Event:
release
-
Statement type:
File details
Details for the file linkml_dumper_utils-1.0.0-py3-none-any.whl.
File metadata
- Download URL: linkml_dumper_utils-1.0.0-py3-none-any.whl
- Upload date:
- Size: 8.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
70338d43103feaf27d149872fa734262644707aebfd61269f1327f31ad0b0022
|
|
| MD5 |
79e691b387637893a2fb5aef9bf121b7
|
|
| BLAKE2b-256 |
cfc8ad87f3c577519ff267da98d742a7bac62aac2119b0947becaef2800c0b93
|
Provenance
The following attestation bundles were made for linkml_dumper_utils-1.0.0-py3-none-any.whl:
Publisher:
python-publish.yml on Cpprentice/linkml-dumper-utils
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
linkml_dumper_utils-1.0.0-py3-none-any.whl -
Subject digest:
70338d43103feaf27d149872fa734262644707aebfd61269f1327f31ad0b0022 - Sigstore transparency entry: 409428762
- Sigstore integration time:
-
Permalink:
Cpprentice/linkml-dumper-utils@d5e9dd7ee5129ba096876c52e472db31b62b54a1 -
Branch / Tag:
refs/tags/v1.0.0 - Owner: https://github.com/Cpprentice
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@d5e9dd7ee5129ba096876c52e472db31b62b54a1 -
Trigger Event:
release
-
Statement type: