Skip to main content

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

linkml_dumper_utils-1.0.0.tar.gz (11.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

linkml_dumper_utils-1.0.0-py3-none-any.whl (8.6 kB view details)

Uploaded Python 3

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

Hashes for linkml_dumper_utils-1.0.0.tar.gz
Algorithm Hash digest
SHA256 9d32b7fad63654c2232d7316b058609ed62c28783890fdc236519c9e8612ac4b
MD5 762611264971cf69fee8b65c12cda86b
BLAKE2b-256 f55253c441d2d7fddf3bdfd1679508867bdcec26521f824cb34ebff493ec3896

See more details on using hashes here.

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

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file linkml_dumper_utils-1.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for linkml_dumper_utils-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 70338d43103feaf27d149872fa734262644707aebfd61269f1327f31ad0b0022
MD5 79e691b387637893a2fb5aef9bf121b7
BLAKE2b-256 cfc8ad87f3c577519ff267da98d742a7bac62aac2119b0947becaef2800c0b93

See more details on using hashes here.

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

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page