Skip to content

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

License

Notifications You must be signed in to change notification settings

hampusnasstrom/ontopint

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

24 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages