ontolutils 0.2.13__tar.gz → 0.2.14__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {ontolutils-0.2.13 → ontolutils-0.2.14}/PKG-INFO +1 -1
- {ontolutils-0.2.13 → ontolutils-0.2.14}/ontolutils/classes/query_util.py +3 -5
- {ontolutils-0.2.13 → ontolutils-0.2.14}/ontolutils/classes/thing.py +3 -3
- {ontolutils-0.2.13 → ontolutils-0.2.14}/ontolutils.egg-info/PKG-INFO +1 -1
- {ontolutils-0.2.13 → ontolutils-0.2.14}/setup.cfg +1 -1
- {ontolutils-0.2.13 → ontolutils-0.2.14}/tests/test_classes.py +34 -5
- {ontolutils-0.2.13 → ontolutils-0.2.14}/LICENSE +0 -0
- {ontolutils-0.2.13 → ontolutils-0.2.14}/README.md +0 -0
- {ontolutils-0.2.13 → ontolutils-0.2.14}/ontolutils/__init__.py +0 -0
- {ontolutils-0.2.13 → ontolutils-0.2.14}/ontolutils/_version.py +0 -0
- {ontolutils-0.2.13 → ontolutils-0.2.14}/ontolutils/cache.py +0 -0
- {ontolutils-0.2.13 → ontolutils-0.2.14}/ontolutils/classes/__init__.py +0 -0
- {ontolutils-0.2.13 → ontolutils-0.2.14}/ontolutils/classes/decorator.py +0 -0
- {ontolutils-0.2.13 → ontolutils-0.2.14}/ontolutils/classes/typing.py +0 -0
- {ontolutils-0.2.13 → ontolutils-0.2.14}/ontolutils/classes/utils.py +0 -0
- {ontolutils-0.2.13 → ontolutils-0.2.14}/ontolutils/namespacelib/__dev.py +0 -0
- {ontolutils-0.2.13 → ontolutils-0.2.14}/ontolutils/namespacelib/__init__.py +0 -0
- {ontolutils-0.2.13 → ontolutils-0.2.14}/ontolutils/namespacelib/_iana_utils.py +0 -0
- {ontolutils-0.2.13 → ontolutils-0.2.14}/ontolutils/namespacelib/codemeta.py +0 -0
- {ontolutils-0.2.13 → ontolutils-0.2.14}/ontolutils/namespacelib/m4i.py +0 -0
- {ontolutils-0.2.13 → ontolutils-0.2.14}/ontolutils/namespacelib/obo.py +0 -0
- {ontolutils-0.2.13 → ontolutils-0.2.14}/ontolutils/namespacelib/pivmeta.py +0 -0
- {ontolutils-0.2.13 → ontolutils-0.2.14}/ontolutils/namespacelib/qudt_kind.py +0 -0
- {ontolutils-0.2.13 → ontolutils-0.2.14}/ontolutils/namespacelib/qudt_unit.py +0 -0
- {ontolutils-0.2.13 → ontolutils-0.2.14}/ontolutils/namespacelib/schema.py +0 -0
- {ontolutils-0.2.13 → ontolutils-0.2.14}/ontolutils/namespacelib/ssno.py +0 -0
- {ontolutils-0.2.13 → ontolutils-0.2.14}/ontolutils.egg-info/SOURCES.txt +0 -0
- {ontolutils-0.2.13 → ontolutils-0.2.14}/ontolutils.egg-info/dependency_links.txt +0 -0
- {ontolutils-0.2.13 → ontolutils-0.2.14}/ontolutils.egg-info/requires.txt +0 -0
- {ontolutils-0.2.13 → ontolutils-0.2.14}/ontolutils.egg-info/top_level.txt +0 -0
- {ontolutils-0.2.13 → ontolutils-0.2.14}/pyproject.toml +0 -0
- {ontolutils-0.2.13 → ontolutils-0.2.14}/setup.py +0 -0
- {ontolutils-0.2.13 → ontolutils-0.2.14}/tests/test_decorator.py +0 -0
- {ontolutils-0.2.13 → ontolutils-0.2.14}/tests/test_namespaces.py +0 -0
- {ontolutils-0.2.13 → ontolutils-0.2.14}/tests/test_query.py +0 -0
- {ontolutils-0.2.13 → ontolutils-0.2.14}/tests/test_readme.py +0 -0
- {ontolutils-0.2.13 → ontolutils-0.2.14}/tests/test_typing.py +0 -0
- {ontolutils-0.2.13 → ontolutils-0.2.14}/tests/test_utils.py +0 -0
- {ontolutils-0.2.13 → ontolutils-0.2.14}/tests/test_version.py +0 -0
|
@@ -295,17 +295,15 @@ def query(cls: Thing,
|
|
|
295
295
|
# in case that the model field names are different than the IRI names, we need to find the
|
|
296
296
|
# corresponding names. The urirefs translate the class model fields to a <prefix>:<name> format.
|
|
297
297
|
# As we have the latter, the inverse dictionary let's us find the model field names.
|
|
298
|
-
|
|
299
|
-
inverse_urirefs = {_v.split(':', 1)[-1]: _k for _k, _v in get_urirefs(cls).items()}
|
|
298
|
+
# inverse_urirefs = {_v.split(':', 1)[-1]: _k for _k, _v in get_urirefs(cls).items()}
|
|
300
299
|
|
|
301
300
|
if limit is not None:
|
|
302
301
|
out = []
|
|
303
302
|
for i, (k, v) in enumerate(kwargs.items()):
|
|
304
|
-
model_field_dict = {inverse_urirefs.get(key, key): value for key, value in v.items()}
|
|
303
|
+
model_field_dict = v # {inverse_urirefs.get(key, key): value for key, value in v.items()}
|
|
305
304
|
if limit == 1:
|
|
306
305
|
return cls.model_validate({'id': k, **model_field_dict})
|
|
307
306
|
out.append(cls.model_validate({'id': k, **model_field_dict}))
|
|
308
307
|
if i == limit:
|
|
309
308
|
return out
|
|
310
|
-
return [cls.model_validate({'id':
|
|
311
|
-
in kwargs.items()]
|
|
309
|
+
return [cls.model_validate({'id': _id, **params}) for _id, params in kwargs.items()]
|
|
@@ -21,7 +21,7 @@ EXTRA = 'allow' # or 'forbid' or 'ignore'
|
|
|
21
21
|
class ThingModel(BaseModel, abc.ABC):
|
|
22
22
|
"""Abstract base model class to be used by model classes used within ontolutils"""
|
|
23
23
|
|
|
24
|
-
model_config = ConfigDict(extra=EXTRA)
|
|
24
|
+
model_config = ConfigDict(extra=EXTRA, populate_by_name=True)
|
|
25
25
|
|
|
26
26
|
@abc.abstractmethod
|
|
27
27
|
def _repr_html_(self) -> str:
|
|
@@ -194,7 +194,7 @@ class Thing(ThingModel):
|
|
|
194
194
|
|
|
195
195
|
def _serialize_fields(
|
|
196
196
|
obj: Union[ThingModel, int, str, float, bool, datetime],
|
|
197
|
-
exclude_none: bool
|
|
197
|
+
exclude_none: bool,
|
|
198
198
|
) -> Union[Dict, int, str, float, bool]:
|
|
199
199
|
"""Serializes the fields of a Thing object into a json-ld
|
|
200
200
|
dictionary (without context!). Note, that IDs can automatically be
|
|
@@ -255,7 +255,7 @@ class Thing(ThingModel):
|
|
|
255
255
|
elif isinstance(v, (int, float)):
|
|
256
256
|
serialized_fields[key] = v
|
|
257
257
|
else:
|
|
258
|
-
serialized_fields[key] = _serialize_fields(v)
|
|
258
|
+
serialized_fields[key] = _serialize_fields(v, exclude_none=exclude_none)
|
|
259
259
|
|
|
260
260
|
_type = URIRefManager[obj.__class__].get(obj.__class__.__name__, obj.__class__.__name__)
|
|
261
261
|
|
|
@@ -5,7 +5,7 @@ import unittest
|
|
|
5
5
|
|
|
6
6
|
import pydantic
|
|
7
7
|
import rdflib
|
|
8
|
-
from pydantic import EmailStr
|
|
8
|
+
from pydantic import EmailStr, Field
|
|
9
9
|
|
|
10
10
|
from ontolutils import Thing, urirefs, namespaces, get_urirefs, get_namespaces
|
|
11
11
|
from ontolutils import set_logging_level
|
|
@@ -31,6 +31,34 @@ class TestNamespaces(unittest.TestCase):
|
|
|
31
31
|
set_logging_level(self.INITIAL_LOG_LEVEL)
|
|
32
32
|
assert logging.getLogger('ontolutils').level == self.INITIAL_LOG_LEVEL
|
|
33
33
|
|
|
34
|
+
def test_model_fields(self):
|
|
35
|
+
@namespaces(foaf="http://xmlns.com/foaf/0.1/")
|
|
36
|
+
@urirefs(Agent='foaf:Agent',
|
|
37
|
+
name='foaf:lastName',
|
|
38
|
+
age='foaf:age')
|
|
39
|
+
class Agent(Thing):
|
|
40
|
+
"""Pydantic Model for http://xmlns.com/foaf/0.1/Agent
|
|
41
|
+
Parameters
|
|
42
|
+
----------
|
|
43
|
+
mbox: EmailStr = None
|
|
44
|
+
Email address (foaf:mbox)
|
|
45
|
+
"""
|
|
46
|
+
name: str = Field(default=None, alias="lastName") # name is synonymous to lastName
|
|
47
|
+
age: int = None
|
|
48
|
+
|
|
49
|
+
agent = Agent(name='John Doe', age=23)
|
|
50
|
+
self.assertEqual(agent.name, 'John Doe')
|
|
51
|
+
self.assertEqual(agent.age, 23)
|
|
52
|
+
|
|
53
|
+
# extra fields are allowed and either the model field or its uriref can be used
|
|
54
|
+
agent = Agent(lastName='Doe', age=23)
|
|
55
|
+
self.assertEqual(agent.name, 'Doe')
|
|
56
|
+
self.assertEqual(agent.age, 23)
|
|
57
|
+
|
|
58
|
+
agent = Agent(age=23)
|
|
59
|
+
self.assertEqual(agent.name, None)
|
|
60
|
+
self.assertEqual(agent.age, 23)
|
|
61
|
+
|
|
34
62
|
def test_resolve_iri(self):
|
|
35
63
|
ret = resolve_iri('foaf:age', context={'foaf': 'http://xmlns.com/foaf/0.1/'})
|
|
36
64
|
self.assertEqual(ret, 'http://xmlns.com/foaf/0.1/age')
|
|
@@ -68,7 +96,7 @@ class TestNamespaces(unittest.TestCase):
|
|
|
68
96
|
lastName='foaf:lastName',
|
|
69
97
|
age='foaf:age')
|
|
70
98
|
class Person(Thing):
|
|
71
|
-
first_name: str = None
|
|
99
|
+
first_name: str = Field(default=None, alias='firstName')
|
|
72
100
|
lastName: str
|
|
73
101
|
age: int = None
|
|
74
102
|
|
|
@@ -165,10 +193,11 @@ class TestNamespaces(unittest.TestCase):
|
|
|
165
193
|
agent.model_validate(agent.model_dump())
|
|
166
194
|
agent.mbox = 'my@email.com'
|
|
167
195
|
jsonld_str1 = agent.model_dump_jsonld(rdflib_serialize=False)
|
|
196
|
+
self.assertTrue('@id' in json.loads(jsonld_str1))
|
|
168
197
|
|
|
169
198
|
self.assertIsInstance(json.loads(jsonld_str1)['foaf:age'], int)
|
|
170
199
|
|
|
171
|
-
jsonld_str2 = agent.model_dump_jsonld(rdflib_serialize=True)
|
|
200
|
+
jsonld_str2 = agent.model_dump_jsonld(rdflib_serialize=True) # will assign blank node! Pop it later
|
|
172
201
|
jsonld_str2_dict = json.loads(jsonld_str2)
|
|
173
202
|
self.assertNotEqual(
|
|
174
203
|
json.loads(jsonld_str1),
|
|
@@ -370,7 +399,7 @@ class TestNamespaces(unittest.TestCase):
|
|
|
370
399
|
"foaf:lastName": "Doe"
|
|
371
400
|
}
|
|
372
401
|
jsonld_dict = json.loads(mt.model_dump_jsonld(resolve_keys=True))
|
|
373
|
-
jsonld_dict.pop('@id')
|
|
402
|
+
jsonld_dict.pop('@id', None)
|
|
374
403
|
self.assertDictEqual(jsonld_dict,
|
|
375
404
|
ref_jsonld)
|
|
376
405
|
|
|
@@ -387,7 +416,7 @@ class TestNamespaces(unittest.TestCase):
|
|
|
387
416
|
"last_name": "Doe"
|
|
388
417
|
}
|
|
389
418
|
jsonld_dict = json.loads(mt.model_dump_jsonld(resolve_keys=False))
|
|
390
|
-
jsonld_dict.pop('@id')
|
|
419
|
+
jsonld_dict.pop('@id', None)
|
|
391
420
|
self.assertDictEqual(jsonld_dict,
|
|
392
421
|
ref_jsonld)
|
|
393
422
|
from pprint import pprint
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|