Data Conversion: Loaders and Dumpers#

The linkml-runtime loaders and dumpers framework provide a way to convert different forms of data that conform to a LinkML Schema.

Dumpers dump an in-memory python object to a serialization format

Loaders load a file or string that is in a supported format, translating into in-memory python objects.

All loaders take a target_class parameter that is the python class we want to reconstitute. Some loaders and dumpers also take a SchemaView as an argument.

The formats supported are:

  • JSON

  • YAML

  • RDF (e.g. Turtle)

  • TSV/CSV

JSON Conversion#

JSON serialization closely follows the in-memory python serialization

class linkml_runtime.dumpers.JSONDumper[source]#
class linkml_runtime.loaders.JSONLoader[source]#
load(*args, **kwargs) BaseModel | YAMLRoot#

Load source as an instance of target_class

Parameters:
  • source – source file/text/url to load

  • target_class – destination class

  • base_dir – scoping directory for source if it is a file or url

  • metadata – metadata about the source

  • _ – extensions

Returns:

instance of target_class

load_any(source: str | dict | TextIO, target_class: Type[BaseModel | YAMLRoot], *, base_dir: str | None = None, metadata: FileInfo | None = None, **_) BaseModel | YAMLRoot | List[BaseModel] | List[YAMLRoot][source]#

Load source as an instance of target_class, or list of instances of target_class

@param source: source file/text/url to load @param target_class: destination class @param base_dir: scoping directory for source if it is a file or url @param metadata: metadata about the source @param _: extensions @return: instance of target_class

loads(source: str, target_class: Type[BaseModel | YAMLRoot], *, metadata: FileInfo | None = None, **_) BaseModel | YAMLRoot#

Load source as a string :param source: source :param target_class: destination class :param metadata: metadata about the source :param _: extensions :return: instance of taarget_class

loads_any(source: str, target_class: Type[BaseModel | YAMLRoot], *, metadata: FileInfo | None = None, **_) BaseModel | YAMLRoot | List[BaseModel] | List[YAMLRoot]#

Load source as a string as an instance of target_class, or list of instances of target_class @param source: source @param target_class: destination class @param metadata: metadata about the source @param _: extensions @return: instance of taarget_class

YAML Conversion#

YAML serialization is essentially the same as JSON

class linkml_runtime.dumpers.YAMLDumper[source]#
dump(element: BaseModel | YAMLRoot, to_file: str, **_) None#

Write element to to_file :param element: LinkML object to be dumped :param to_file: file to dump to :@param _: method specific arguments

dumps(element: BaseModel | YAMLRoot, **kwargs) str[source]#

Return element formatted as a YAML string

class linkml_runtime.loaders.YAMLLoader[source]#

A Loader that is capable of instantiating LinkML data objects from a YAML file

load(*args, **kwargs) BaseModel | YAMLRoot#

Load source as an instance of target_class

Parameters:
  • source – source file/text/url to load

  • target_class – destination class

  • base_dir – scoping directory for source if it is a file or url

  • metadata – metadata about the source

  • _ – extensions

Returns:

instance of target_class

load_any(source: str | dict | TextIO, target_class: Type[YAMLRoot] | Type[BaseModel], *, base_dir: str | None = None, metadata: FileInfo | None = None, **_) YAMLRoot | List[YAMLRoot][source]#

Load source as an instance of target_class, or list of instances of target_class

@param source: source file/text/url to load @param target_class: destination class @param base_dir: scoping directory for source if it is a file or url @param metadata: metadata about the source @param _: extensions @return: instance of target_class

loads(source: str, target_class: Type[BaseModel | YAMLRoot], *, metadata: FileInfo | None = None, **_) BaseModel | YAMLRoot#

Load source as a string :param source: source :param target_class: destination class :param metadata: metadata about the source :param _: extensions :return: instance of taarget_class

loads_any(source: str, target_class: Type[BaseModel | YAMLRoot], *, metadata: FileInfo | None = None, **_) BaseModel | YAMLRoot | List[BaseModel] | List[YAMLRoot][source]#

Load source as a string @param source: source @param target_class: destination class @param metadata: metadata about the source @param _: extensions @return: instance of taarget_class

RDF Conversion#

class linkml_runtime.dumpers.RDFLibDumper[source]#

Dumps from elements (instances of a LinkML model) to an rdflib Graph

Note: this should be used in place of rdf_loader for now

This requires a SchemaView object

dump(element: BaseModel | YAMLRoot, to_file: str, schemaview: SchemaView = None, fmt: str = 'turtle', prefix_map: Dict[str, str] | Converter | None = None, **args) None[source]#

Write element as rdf to to_file

Parameters:
  • element – element to represent in RDF

  • to_file

  • schemaview

  • fmt

  • prefix_map

Returns:

dumps(element: BaseModel | YAMLRoot, schemaview: SchemaView = None, fmt: str | None = 'turtle', prefix_map: Dict[str, str] | Converter | None = None) str[source]#

Convert element into an RDF graph guided by the schema

Parameters:
  • element

  • schemaview

  • fmt

  • prefix_map

Returns:

serialization of rdflib Graph containing element

class linkml_runtime.loaders.RDFLibLoader[source]#

Loads objects from rdflib Graphs into the python target_class structure

Note: this is a more complete replacement for rdf_loader

load(source: str | TextIO | Graph, target_class: Type[BaseModel | YAMLRoot], *, schemaview: SchemaView = None, prefix_map: Dict[str, str] | Converter | None = None, fmt: str | None = 'turtle', metadata: FileInfo | None = None, **kwargs) BaseModel | YAMLRoot[source]#

Load the RDF in source into the python target_class structure

The assumption of all loaders is that the source contains exactly one instance of the target class. To load from graphs with multiple instances, use from_rdf_graph

Parameters:
  • source – RDF data source. Can be a file name, an open handle or an existing graph

  • target_class – LinkML class to load the RDF into

  • schemaview – view over schema to guide instantiation

  • prefix_map – map of prefixes used in data

  • fmt – format of source if it isn’t an existing Graph

  • metadata – source information. Used by some loaders to record where information came from

  • kwargs – additional arguments passed to from_rdf_graph

Returns:

Instance of target_class

load_any(source: str, **kwargs) BaseModel | YAMLRoot | List[BaseModel] | List[YAMLRoot][source]#

Load source as an instance of target_class, or list of instances of target_class

@param source: source file/text/url to load @param target_class: destination class @param base_dir: scoping directory for source if it is a file or url @param metadata: metadata about the source @param _: extensions @return: instance of target_class

loads(source: str, **kwargs) BaseModel | YAMLRoot[source]#

Load source as a string :param source: source :param target_class: destination class :param metadata: metadata about the source :param _: extensions :return: instance of taarget_class

loads_any(source: str, target_class: Type[BaseModel | YAMLRoot], *, metadata: FileInfo | None = None, **_) BaseModel | YAMLRoot | List[BaseModel] | List[YAMLRoot]#

Load source as a string as an instance of target_class, or list of instances of target_class @param source: source @param target_class: destination class @param metadata: metadata about the source @param _: extensions @return: instance of taarget_class

CSV Conversion#

class linkml_runtime.dumpers.CSVDumper[source]#
dump(element: BaseModel | YAMLRoot, to_file: str, **_) None#

Write element to to_file :param element: LinkML object to be dumped :param to_file: file to dump to :@param _: method specific arguments

dumps(element: BaseModel | YAMLRoot, index_slot: SlotDefinitionName = None, schema: SchemaDefinition = None, schemaview: SchemaView = None, **kwargs) str#

Return element formatted as CSV lines

class linkml_runtime.loaders.CSVLoader[source]#
load(source: str, target_class: Type[BaseModel | YAMLRoot], index_slot: SlotDefinitionName = None, schema: SchemaDefinition = None, schemaview: SchemaView = None, **kwargs) str#

Load source as an instance of target_class

Parameters:
  • source – source file/text/url to load

  • target_class – destination class

  • base_dir – scoping directory for source if it is a file or url

  • metadata – metadata about the source

  • _ – extensions

Returns:

instance of target_class

load_any(*args, **kwargs) YAMLRoot | List[YAMLRoot]#

Load source as an instance of target_class, or list of instances of target_class

@param source: source file/text/url to load @param target_class: destination class @param base_dir: scoping directory for source if it is a file or url @param metadata: metadata about the source @param _: extensions @return: instance of target_class

loads(input, target_class: Type[BaseModel | YAMLRoot], index_slot: SlotDefinitionName = None, schema: SchemaDefinition = None, schemaview: SchemaView = None, **kwargs) str#

Load source as a string :param source: source :param target_class: destination class :param metadata: metadata about the source :param _: extensions :return: instance of taarget_class

loads_any(source: str, target_class: Type[BaseModel | YAMLRoot], *, metadata: FileInfo | None = None, **_) BaseModel | YAMLRoot | List[BaseModel] | List[YAMLRoot]#

Load source as a string as an instance of target_class, or list of instances of target_class @param source: source @param target_class: destination class @param metadata: metadata about the source @param _: extensions @return: instance of taarget_class