-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from hampusnasstrom/add-serializer
Add serializer
- Loading branch information
Showing
12 changed files
with
398 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# This workflow will run pytest only | ||
|
||
name: Run pytest | ||
|
||
on: | ||
push: | ||
pull_request: | ||
workflow_dispatch: | ||
|
||
|
||
permissions: | ||
contents: read | ||
|
||
jobs: | ||
deploy: | ||
|
||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- uses: actions/checkout@v3 | ||
- name: Set up Python | ||
uses: actions/setup-python@v3 | ||
with: | ||
python-version: '3.x' | ||
- name: Install dependencies | ||
run: | | ||
python -m pip install --upgrade pip | ||
pip install .[dev] | ||
- name: Test with pytest | ||
run: | | ||
pytest |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,75 @@ | ||
# ontopint | ||
A python package for reading units from a JSON-LD files and generating pint quantities. | ||
A python package for reading & writing units from a JSON-LD files and generating pint quantities. | ||
|
||
## How it works | ||
|
||
```python | ||
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](https://github.com/hampusnasstrom/ontopint/tree/main/tests/data) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
|
||
import pint | ||
from ontopint import get_qunit_iri_from_unit_code, get_ucum_code_from_unit_iri | ||
import ontopint | ||
|
||
def test_pint_print_formats(): | ||
# see https://pint.readthedocs.io/en/stable/user/formatting.html | ||
q : pint.Quantity = pint.Quantity(1.0, ontopint.ureg.from_ucum("kg")).to_base_units() | ||
assert( float(format(q, 'f~').split(' ')[0]) == 1.0) | ||
assert( format(q.u, '~') == "kg") | ||
q : pint.Quantity = pint.Quantity(304, ontopint.ureg.from_ucum("cm")) | ||
assert( float(format(q, 'f~').split(' ')[0]) == 304) | ||
assert( format(q, 'f~').split(' ')[1] == "cm") | ||
q : pint.Quantity = pint.Quantity(10, ontopint.ureg.from_ucum("eV")) | ||
assert( float(format(q, 'f~').split(' ')[0]) == 10) | ||
assert( format(q.u, '~') == "eV") | ||
|
||
def test_qudt_sparql_api(): | ||
assert (get_qunit_iri_from_unit_code("kg") == "http://qudt.org/vocab/unit/KiloGM") | ||
assert (get_qunit_iri_from_unit_code("kg", True) == "http://qudt.org/vocab/unit/KiloGM") | ||
assert (get_ucum_code_from_unit_iri("http://qudt.org/vocab/unit/KiloGM") == "kg") | ||
|
||
assert (get_qunit_iri_from_unit_code("m") == "http://qudt.org/vocab/unit/M") | ||
assert (get_qunit_iri_from_unit_code("m", True) == "http://qudt.org/vocab/unit/M") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import ontopint | ||
import pint | ||
|
||
from common import _load_test_data, _recursive_items | ||
|
||
def test_default_keys(): | ||
"""test input data with default keys 'value' and 'unit' | ||
""" | ||
input_jsonld = _load_test_data("test_data_default_keys.jsonld") | ||
parsed_jsonld = ontopint.parse_units(input_jsonld) | ||
del parsed_jsonld["@context"] | ||
parse_values_count = 0 | ||
for key, value in _recursive_items(parsed_jsonld): | ||
if key == "value": | ||
assert(isinstance(value, pint.Quantity)) | ||
parse_values_count += 1 | ||
if key == "unit": assert False, "unit key should not be present" | ||
assert parse_values_count == 2, "result should contain 2 parsed values" | ||
|
||
def test_custom_keys(): | ||
"""test input data with custom keys 'my_value' and 'my_unit' | ||
""" | ||
input_jsonld = _load_test_data("test_data_custom_keys.jsonld") | ||
parsed_jsonld = ontopint.parse_units(input_jsonld) | ||
del parsed_jsonld["@context"] | ||
parse_values_count = 0 | ||
for key, value in _recursive_items(parsed_jsonld): | ||
if key == "my_value": | ||
assert(isinstance(value, pint.Quantity)) | ||
parse_values_count += 1 | ||
if key == "my_unit": assert False, "my_unit key should not be present" | ||
assert parse_values_count == 2, "result should contain 2 parsed values" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import ontopint | ||
import deepdiff | ||
|
||
def test_default_keys(): | ||
"""test input data with default keys 'value' and 'unit' | ||
""" | ||
|
||
test = { | ||
"value": ontopint.ureg.Quantity( | ||
1.123, ontopint.ureg.from_ucum("eV") | ||
) | ||
} | ||
expected = { | ||
"value": 1.123, | ||
"unit": "qunit:EV" | ||
} | ||
result = ontopint.export_units(test) | ||
del result["@context"] | ||
assert (len(deepdiff.DeepDiff(expected, result).keys()) == 0) # no diff | ||
|
||
def test_custom_keys(): | ||
"""test input data with custom keys 'my_value' and 'my_unit' | ||
""" | ||
test = { | ||
"@context": { | ||
"qudt": "http://qudt.org/schema/qudt/", | ||
"qunit": "http://qudt.org/vocab/unit/", | ||
"qkind": "http://qudt.org/vocab/quantkind/", | ||
"my_unit": { | ||
"@id": "qudt:hasUnit", | ||
"@type": "@id" | ||
}, | ||
"my_value": "qudt:value", | ||
}, | ||
"my_value": ontopint.ureg.Quantity( | ||
1.123, ontopint.ureg.from_ucum("eV") | ||
) | ||
} | ||
expected = { | ||
"my_value": 1.123, | ||
"my_unit": "qunit:EV" | ||
} | ||
result = ontopint.export_units(test) | ||
del result["@context"] | ||
assert (len(deepdiff.DeepDiff(expected, result).keys()) == 0) # no diff | ||
|
||
|
Oops, something went wrong.