hampusnasstrom / ontopint

A python package for reading units from a JSON-LD files and generating pint quantities.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

ontopint

A python package for reading & writing units from a JSON-LD files and generating pint quantities.

How it works

import ontopint

# jsonld input with 'value' and 'unit' mapped to qudt terms
data = {
  "@context": {
    "qudt": "http://qudt.org/schema/qudt/",
    "qunit": "http://qudt.org/vocab/unit/",
    "qkind": "http://qudt.org/vocab/quantkind/",
    "unit": {
      "@id": "qudt:hasUnit",
      "@type": "@id"
    },
    "quantity": {
      "@id": "qudt:hasQuantityKind",
      "@type": "@id"
    },
    "value": "qudt:value"
  },
  "value": 4.0,
  "unit": "qunit:CentiM"
}

# convert the value + unit pairs to pint.Quantity
data = ontopint.parse_units(data)
print(data)
"""
{
  '@context': {...},
  'value': <Quantity(4.0, 'centimeter')>
}
"""

# do something with pint
data["value"] += 3 * ontopint.ureg.meter
data["value"] = data["value"].to(ontopint.ureg.meter)
print(data)
"""
{
  '@context': {...},
  'value': <Quantity(3.04, 'meter')>
}
"""

# export the result as jsonld
data = ontopint.export_units(data)
print(data)
"""
{
  "@context": {
    "qudt": "http://qudt.org/schema/qudt/",
    "qunit": "http://qudt.org/vocab/unit/",
    "qkind": "http://qudt.org/vocab/quantkind/",
    "unit": {
      "@id": "qudt:hasUnit",
      "@type": "@id"
    },
    "quantity": {
      "@id": "qudt:hasQuantityKind",
      "@type": "@id"
    },
    "value": "qudt:value"
  },
  "value": 3.04,
  "unit": "qunit:M"
}
"""

Note: more complex examples can be found at tests/data

About

A python package for reading units from a JSON-LD files and generating pint quantities.

License:MIT License


Languages

Language:Python 100.0%