Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
Added first version of JSONDecoder and pyproject.toml

---------

Co-authored-by: Fabian Kirchner <[email protected]>

Co-authored-by: Simon Stier <[email protected]>
  • Loading branch information
hampusnasstrom and simontaurus committed Apr 24, 2024
1 parent 062f967 commit 4a643c4
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 0 deletions.
90 changes: 90 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
[build-system]
requires = [
"setuptools>=61.0.0",
"setuptools-scm>=8.0",
]
build-backend = "setuptools.build_meta"

[project]
name = "ontopint"
dynamic = ["version"]
description = "A python package for reading units from a JSON-LD files and generating pint quantities."
readme = "README.md"
authors = [
{ name = "Hampus Näsström", email = '[email protected]' },
{ name = "Fabian Kirchner" },
{ name = "Simon Stier" },
]
maintainers = [
{ name = "Hampus Näsström", email = '[email protected]' }
]
classifiers = [
"Development Status :: 4 - Beta",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3 :: Only",
"License :: OSI Approved :: MIT License",
]
dependencies = [
"rdflib",
"pint",
]
[project.optional-dependencies]
dev = [
"pytest",
]

[project.license]
file = "LICENSE"

[project.urls]
"Homepage" = "https://github.com/hampusnasstrom/ontopint"
"Bug Tracker" = "https://github.com/hampusnasstrom/ontopint/issues"

[tool.ruff]
include = ["src/*.py", "tests/*.py"]
select = [
"E", # pycodestyle
"W", # pycodestyle
"PL", # pylint
]
ignore = [
"E501", # Line too long ({width} > {limit} characters)
"E701", # Multiple statements on one line (colon)
"E731", # Do not assign a lambda expression, use a def
"E402", # Module level import not at top of file
"PLR0911", # Too many return statements
"PLR0912", # Too many branches
"PLR0913", # Too many arguments in function definition
"PLR0915", # Too many statements
"PLR2004", # Magic value used instead of constant
"PLW0603", # Using the global statement
"PLW2901", # redefined-loop-name
"PLR1714", # consider-using-in
"PLR5501", # else-if-used
]
fixable = ["ALL"]
exclude = ["dependencies"]

# Same as Black.
line-length = 88
indent-width = 4

[tool.ruff.format]
# use single quotes for strings.
quote-style = "single"

# indent with spaces, rather than tabs.
indent-style = "space"

# Like Black, respect magic trailing commas.
skip-magic-trailing-comma = false

# Like Black, automatically detect the appropriate line ending.
line-ending = "auto"

[tool.setuptools.packages.find]
where = [
"src",
]

[tool.setuptools_scm]
27 changes: 27 additions & 0 deletions src/ontopint/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import json
import rdflib
from pint import UnitRegistry

ureg = UnitRegistry()

class UnitDecoder(json.JSONDecoder):
def __init__(self, *args, **kwargs):
json.JSONDecoder.__init__(self, object_hook=self.object_hook, *args, **kwargs)

def object_hook(self, obj):
if 'unit' in obj and 'value' in obj:
return ureg.Quantity(obj['value'], obj['unit'])
unit = None
unit_defs = [v for k, v in obj.items() if isinstance(v, str) and v.startswith('qudt:')]
if len(unit_defs) > 1:
raise ValueError('More that one unit definition')
if unit_defs:
iri = unit_defs[0].replace('qudt:', 'http://qudt.org/vocab/unit/')
graph = rdflib.Graph()
graph.parse(iri)
result = graph.query(f"SELECT * WHERE {{<{iri}> <http://qudt.org/schema/qudt/symbol> ?symbol}}")
unit = result.bindings[0]['symbol']
for k, v in obj.items():
if isinstance(v, float):
obj[k] = ureg.Quantity(v, unit)
return obj

0 comments on commit 4a643c4

Please sign in to comment.