Standard Name

import ssnolib

Create a standard name

As a minimum, standard_name, unit and description must be provided. You can also assign the standard name table, it is associated with. Let’s start with the core properties, though:

sn = ssnolib.StandardName(
    standardName='x_velocity',
    unit='m/s',
    description=['The velocity in x-axis direction.@en', 'Die Geschwindigeit in x-Richtung des Koordinatensystems.@de']
)
print(sn.serialize(format="ttl"))
@prefix ssno: <https://matthiasprobst.github.io/ssno#> .

[] a ssno:StandardName ;
    ssno:description "Die Geschwindigeit in x-Richtung des Koordinatensystems."@de,
        "The velocity in x-axis direction."@en ;
    ssno:standardName "x_velocity" ;
    ssno:unit <http://qudt.org/vocab/unit/M-PER-SEC> .

We automatically get an ID assigned, with is a blank URI:

sn
StandardName(id=_:N2dfb82558fdf4026b03bd7eb678af173, description=[LangString(value='The velocity in x-axis direction.', lang='en'), LangString(value='Die Geschwindigeit in x-Richtung des Koordinatensystems.', lang='de')], standardName=x_velocity, unit=http://qudt.org/vocab/unit/M-PER-SEC)

Aliases

Note, that we used the field names according to the URI of the ontology. E.g. we used “standardName” for “https://matthiasprobst.github.io/ssno#standardName” and “unit” for “”https://matthiasprobst.github.io/ssno#unit”. However, this is unusual syntax for pythonic naming. In Python, we prefer using lowercase with underscores (snake_case) for variable names, whereas the ontology preferred camelCase. As The Python object connects both worlds, both versions are allowed to be used interchangible:

sn = ssnolib.StandardName(
    standard_name='x_velocity',
    unit='m/s',
    description='The velocity in x-axis direction'
)
sn
StandardName(id=_:N5cc1615fcbbd4680b460ba35065ae3eb, description=The velocity in x-axis direction, standardName=x_velocity, unit=http://qudt.org/vocab/unit/M-PER-SEC)

Side note: Invalid fields are not allowed and are reported:

import pydantic
try:
    sn = ssnolib.StandardName(
        name='x_velocity',
        units='m/s',
        description='The velocity in x-axis direction@en'
    )
except pydantic.ValidationError as e:
    print(e)
2 validation errors for StandardName
standard_name
  Field required [type=missing, input_value={'name': 'x_velocity', 'u...in x-axis direction@en'}, input_type=dict]
    For further information visit https://errors.pydantic.dev/2.10/v/missing
unit
  Field required [type=missing, input_value={'name': 'x_velocity', 'u...in x-axis direction@en'}, input_type=dict]
    For further information visit https://errors.pydantic.dev/2.10/v/missing

Validation

Fields are validated by their types and sometimes downstream validator functions. This means, that it is not possible to assign an integer to the definition. The standard name is validated mostly using regex. The units value is translated to a URI using the QUDT ontology. Here are some examples:

# Wrong type for definition:
try:
    sn = ssnolib.StandardName(
        standard_name='x_velocity',
        unit='m/s',
        description=123
    )
except pydantic.ValidationError as e:
    print(e)
2 validation errors for StandardName
description.LangString
  Input should be a valid dictionary or instance of LangString [type=model_type, input_value=123, input_type=int]
    For further information visit https://errors.pydantic.dev/2.10/v/model_type
description.list[LangString]
  Input should be a valid list [type=list_type, input_value=123, input_type=int]
    For further information visit https://errors.pydantic.dev/2.10/v/list_type
# Incorrect standard name that does not match the basic pattern:
try:
    sn = ssnolib.StandardName(
        standard_name='X_velocity_',
        unit='m/s',
        description="invalid standard name"
    )
except pydantic.ValidationError as e:
    print(e)
1 validation error for StandardName
standard_name
  Value error, Invalid standard name 'X_velocity_' according to the core pattern '^[a-z0-9]+(?:_[a-z0-9]+)*$'. [type=value_error, input_value='X_velocity_', input_type=str]
    For further information visit https://errors.pydantic.dev/2.10/v/value_error
# Cannot parse unit
try:
    sn = ssnolib.StandardName(
        standard_name='x_velocity',
        unit='meterprosec',
        description="invalid standard name"
    )
except pydantic.ValidationError as e:
    print(e)
1 validation error for StandardName
unit.unit
  Value error, your_message Unable to parse: "meterprosec" of standard name "x_velocity" [type=value_error, input_value='meterprosec', input_type=str]
    For further information visit https://errors.pydantic.dev/2.10/v/value_error

Representations

A standard name object can be represented in various formats, such as TTL (turtle), XML or JSON-LD (or any serialization defined by rdflib, which is used under the hood, see here for more)

TTL/Turtle

print(sn.serialize(format="ttl"))
@prefix ssno: <https://matthiasprobst.github.io/ssno#> .

[] a ssno:StandardName ;
    ssno:description "The velocity in x-axis direction" ;
    ssno:standardName "x_velocity" ;
    ssno:unit <http://qudt.org/vocab/unit/M-PER-SEC> .

XML

print(sn.serialize(format="xml"))
<?xml version="1.0" encoding="utf-8"?>
<rdf:RDF
   xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
   xmlns:ssno="https://matthiasprobst.github.io/ssno#"
>
  <rdf:Description rdf:nodeID="N5cc1615fcbbd4680b460ba35065ae3eb">
    <rdf:type rdf:resource="https://matthiasprobst.github.io/ssno#StandardName"/>
    <ssno:description>The velocity in x-axis direction</ssno:description>
    <ssno:standardName>x_velocity</ssno:standardName>
    <ssno:unit rdf:resource="http://qudt.org/vocab/unit/M-PER-SEC"/>
  </rdf:Description>
</rdf:RDF>

JSON-LD

print(sn.serialize(format="json-ld"))
{
  "@context": {
    "dcat": "http://www.w3.org/ns/dcat#",
    "dcterms": "http://purl.org/dc/terms/",
    "owl": "http://www.w3.org/2002/07/owl#",
    "rdfs": "http://www.w3.org/2000/01/rdf-schema#",
    "schema": "https://schema.org/",
    "skos": "http://www.w3.org/2004/02/skos/core#",
    "ssno": "https://matthiasprobst.github.io/ssno#"
  },
  "@id": "_:N5cc1615fcbbd4680b460ba35065ae3eb",
  "@type": "ssno:StandardName",
  "ssno:description": "The velocity in x-axis direction",
  "ssno:standardName": "x_velocity",
  "ssno:unit": {
    "@id": "http://qudt.org/vocab/unit/M-PER-SEC"
  }
}