From 4a643c44b0f147171ed0d214c776d5e61646a168 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hampus=20N=C3=A4sstr=C3=B6m?= Date: Wed, 24 Apr 2024 14:23:35 +0200 Subject: [PATCH] Initial commit Added first version of JSONDecoder and pyproject.toml --------- Co-authored-by: Fabian Kirchner Co-authored-by: Simon Stier --- pyproject.toml | 90 ++++++++++++++++++++++++++++++++++++++++ src/ontopint/__init__.py | 27 ++++++++++++ 2 files changed, 117 insertions(+) create mode 100644 pyproject.toml create mode 100644 src/ontopint/__init__.py diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 0000000..5a39cb6 --- /dev/null +++ b/pyproject.toml @@ -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 = 'hampus.naesstroem@physik.hu-berlin.de' }, + { name = "Fabian Kirchner" }, + { name = "Simon Stier" }, +] +maintainers = [ + { name = "Hampus Näsström", email = 'hampus.naesstroem@physik.hu-berlin.de' } +] +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] \ No newline at end of file diff --git a/src/ontopint/__init__.py b/src/ontopint/__init__.py new file mode 100644 index 0000000..6c9213f --- /dev/null +++ b/src/ontopint/__init__.py @@ -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}> ?symbol}}") + unit = result.bindings[0]['symbol'] + for k, v in obj.items(): + if isinstance(v, float): + obj[k] = ureg.Quantity(v, unit) + return obj