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]#
dump(element: Union[BaseModel, YAMLRoot], to_file: str, contexts: Optional[Union[str, dict, JsonObj, List[Union[str, dict, JsonObj]]]] = None, **kwargs) None[source]#

Write element as json to to_file :param element: LinkML object to be serialized as YAML :param to_file: file to write to :param contexts: JSON-LD context(s) in the form of:

  • file name

  • URL

  • JSON String

  • dict

  • JSON Object

  • A list containing elements of any type named above

dumps(element: Union[BaseModel, YAMLRoot], contexts: Optional[Union[str, dict, JsonObj, List[Union[str, dict, JsonObj]]]] = None, inject_type=True) str[source]#

Return element as a JSON or a JSON-LD string :param element: LinkML object to be emitted :param contexts: JSON-LD context(s) in the form of:

  • file name

  • URL

  • JSON String

  • dict

  • JSON Object

  • A list containing elements of any type named above

Parameters:

inject_type – if True (default), add a @type at the top level

Returns:

JSON Object representing the element

class linkml_runtime.loaders.JSONLoader[source]#
load(*args, **kwargs) Union[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: Union[str, dict, TextIO], target_class: Type[Union[BaseModel, YAMLRoot]], *, base_dir: Optional[str] = None, metadata: Optional[FileInfo] = None, **_) Union[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[Union[BaseModel, YAMLRoot]], *, metadata: Optional[FileInfo] = None, **_) Union[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[Union[BaseModel, YAMLRoot]], *, metadata: Optional[FileInfo] = None, **_) Union[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: Union[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: Union[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) Union[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: Union[str, dict, TextIO], target_class: Union[Type[YAMLRoot], Type[BaseModel]], *, base_dir: Optional[str] = None, metadata: Optional[FileInfo] = None, **_) Union[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[Union[BaseModel, YAMLRoot]], *, metadata: Optional[FileInfo] = None, **_) Union[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[Union[BaseModel, YAMLRoot]], *, metadata: Optional[FileInfo] = None, **_) Union[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.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: Union[str, TextIO, Graph], target_class: Type[Union[BaseModel, YAMLRoot]], *, schemaview: Optional[SchemaView] = None, prefix_map: Optional[Union[Dict[str, str], Converter]] = None, fmt: Optional[str] = 'turtle', metadata: Optional[FileInfo] = None, **kwargs) Union[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) Union[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) Union[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[Union[BaseModel, YAMLRoot]], *, metadata: Optional[FileInfo] = None, **_) Union[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: Union[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: Union[BaseModel, YAMLRoot], index_slot: Optional[SlotDefinitionName] = None, schema: Optional[SchemaDefinition] = None, schemaview: Optional[SchemaView] = None, **kwargs) str#

Return element formatted as CSV lines

class linkml_runtime.loaders.CSVLoader[source]#
load(source: str, target_class: Type[Union[BaseModel, YAMLRoot]], index_slot: Optional[SlotDefinitionName] = None, schema: Optional[SchemaDefinition] = None, schemaview: Optional[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) Union[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[Union[BaseModel, YAMLRoot]], index_slot: Optional[SlotDefinitionName] = None, schema: Optional[SchemaDefinition] = None, schemaview: Optional[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[Union[BaseModel, YAMLRoot]], *, metadata: Optional[FileInfo] = None, **_) Union[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