Skip to main content

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

Project description

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

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

ontopint-0.1.1.tar.gz (9.8 kB view hashes)

Uploaded Source

Built Distribution

ontopint-0.1.1-py3-none-any.whl (6.1 kB view hashes)

Uploaded Python 3

Supported by

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