ontolutils 0.2.8__tar.gz → 0.2.9__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.8 → ontolutils-0.2.9}/PKG-INFO +1 -1
- {ontolutils-0.2.8 → ontolutils-0.2.9}/ontolutils/__init__.py +4 -1
- ontolutils-0.2.9/ontolutils/classes/__init__.py +6 -0
- {ontolutils-0.2.8 → ontolutils-0.2.9}/ontolutils/classes/decorator.py +1 -0
- {ontolutils-0.2.8 → ontolutils-0.2.9}/ontolutils/classes/thing.py +171 -24
- {ontolutils-0.2.8 → ontolutils-0.2.9}/ontolutils.egg-info/PKG-INFO +1 -1
- {ontolutils-0.2.8 → ontolutils-0.2.9}/setup.cfg +1 -1
- {ontolutils-0.2.8 → ontolutils-0.2.9}/tests/test_classes.py +44 -7
- {ontolutils-0.2.8 → ontolutils-0.2.9}/tests/test_readme.py +14 -1
- ontolutils-0.2.8/ontolutils/classes/__init__.py +0 -6
- {ontolutils-0.2.8 → ontolutils-0.2.9}/LICENSE +0 -0
- {ontolutils-0.2.8 → ontolutils-0.2.9}/README.md +0 -0
- {ontolutils-0.2.8 → ontolutils-0.2.9}/ontolutils/_version.py +0 -0
- {ontolutils-0.2.8 → ontolutils-0.2.9}/ontolutils/cache.py +0 -0
- {ontolutils-0.2.8 → ontolutils-0.2.9}/ontolutils/classes/query_util.py +0 -0
- {ontolutils-0.2.8 → ontolutils-0.2.9}/ontolutils/classes/typing.py +0 -0
- {ontolutils-0.2.8 → ontolutils-0.2.9}/ontolutils/classes/utils.py +0 -0
- {ontolutils-0.2.8 → ontolutils-0.2.9}/ontolutils/namespacelib/__dev.py +0 -0
- {ontolutils-0.2.8 → ontolutils-0.2.9}/ontolutils/namespacelib/__init__.py +0 -0
- {ontolutils-0.2.8 → ontolutils-0.2.9}/ontolutils/namespacelib/_iana_utils.py +0 -0
- {ontolutils-0.2.8 → ontolutils-0.2.9}/ontolutils/namespacelib/codemeta.py +0 -0
- {ontolutils-0.2.8 → ontolutils-0.2.9}/ontolutils/namespacelib/m4i.py +0 -0
- {ontolutils-0.2.8 → ontolutils-0.2.9}/ontolutils/namespacelib/obo.py +0 -0
- {ontolutils-0.2.8 → ontolutils-0.2.9}/ontolutils/namespacelib/pivmeta.py +0 -0
- {ontolutils-0.2.8 → ontolutils-0.2.9}/ontolutils/namespacelib/qudt_kind.py +0 -0
- {ontolutils-0.2.8 → ontolutils-0.2.9}/ontolutils/namespacelib/qudt_unit.py +0 -0
- {ontolutils-0.2.8 → ontolutils-0.2.9}/ontolutils/namespacelib/schema.py +0 -0
- {ontolutils-0.2.8 → ontolutils-0.2.9}/ontolutils/namespacelib/ssno.py +0 -0
- {ontolutils-0.2.8 → ontolutils-0.2.9}/ontolutils.egg-info/SOURCES.txt +0 -0
- {ontolutils-0.2.8 → ontolutils-0.2.9}/ontolutils.egg-info/dependency_links.txt +0 -0
- {ontolutils-0.2.8 → ontolutils-0.2.9}/ontolutils.egg-info/requires.txt +0 -0
- {ontolutils-0.2.8 → ontolutils-0.2.9}/ontolutils.egg-info/top_level.txt +0 -0
- {ontolutils-0.2.8 → ontolutils-0.2.9}/pyproject.toml +0 -0
- {ontolutils-0.2.8 → ontolutils-0.2.9}/setup.py +0 -0
- {ontolutils-0.2.8 → ontolutils-0.2.9}/tests/test_decorator.py +0 -0
- {ontolutils-0.2.8 → ontolutils-0.2.9}/tests/test_namespaces.py +0 -0
- {ontolutils-0.2.8 → ontolutils-0.2.9}/tests/test_query.py +0 -0
- {ontolutils-0.2.8 → ontolutils-0.2.9}/tests/test_typing.py +0 -0
- {ontolutils-0.2.8 → ontolutils-0.2.9}/tests/test_utils.py +0 -0
- {ontolutils-0.2.8 → ontolutils-0.2.9}/tests/test_version.py +0 -0
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
import logging
|
|
3
3
|
|
|
4
4
|
from ._version import __version__
|
|
5
|
-
from .classes import Thing
|
|
5
|
+
from .classes import Thing, get_urirefs, get_namespaces
|
|
6
6
|
from .classes import namespaces, urirefs
|
|
7
7
|
from .classes import query, dquery
|
|
8
8
|
from .classes.utils import merge_jsonld
|
|
@@ -35,4 +35,7 @@ __all__ = ['Thing',
|
|
|
35
35
|
'query',
|
|
36
36
|
'set_logging_level',
|
|
37
37
|
'merge_jsonld',
|
|
38
|
+
'dquery',
|
|
39
|
+
'get_urirefs',
|
|
40
|
+
'get_namespaces',
|
|
38
41
|
]
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
"""Classes for working with ontology data"""
|
|
2
|
+
from .decorator import namespaces, urirefs
|
|
3
|
+
from .query_util import query, dquery
|
|
4
|
+
from .thing import Thing, get_urirefs, get_namespaces
|
|
5
|
+
|
|
6
|
+
__all__ = ['namespaces', 'urirefs', 'Thing', 'query', 'dquery', 'get_urirefs', 'get_namespaces']
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import abc
|
|
2
2
|
import json
|
|
3
3
|
import logging
|
|
4
|
+
import pathlib
|
|
4
5
|
import rdflib
|
|
5
6
|
from datetime import datetime
|
|
6
7
|
from pydantic import HttpUrl, FileUrl, BaseModel, ConfigDict
|
|
@@ -25,11 +26,69 @@ class ThingModel(BaseModel, abc.ABC):
|
|
|
25
26
|
"""Returns the HTML representation of the class"""
|
|
26
27
|
|
|
27
28
|
|
|
29
|
+
def resolve_iri(iri: str, context: Dict) -> str:
|
|
30
|
+
"""Resolves short iri string which uses prefix, e.g.
|
|
31
|
+
converts 'foaf:firstName' to 'http://xmlns.com/foaf/0.1/firstName'
|
|
32
|
+
if the prefix is not found in context, the input iri string is
|
|
33
|
+
returned.
|
|
34
|
+
"""
|
|
35
|
+
if iri.startswith('http'):
|
|
36
|
+
return iri
|
|
37
|
+
ns, key = split_URIRef(iri)
|
|
38
|
+
prefix_iri = context.get(ns, None)
|
|
39
|
+
if prefix_iri is None:
|
|
40
|
+
return iri
|
|
41
|
+
return f'{prefix_iri}{key}'
|
|
42
|
+
|
|
43
|
+
|
|
28
44
|
@namespaces(owl='http://www.w3.org/2002/07/owl#',
|
|
29
45
|
rdfs='http://www.w3.org/2000/01/rdf-schema#')
|
|
30
46
|
@urirefs(Thing='owl:Thing', label='rdfs:label')
|
|
31
47
|
class Thing(ThingModel):
|
|
32
|
-
"""owl:Thing
|
|
48
|
+
"""Most basic concept class owl:Thing (see also https://www.w3.org/TR/owl-guide/)
|
|
49
|
+
|
|
50
|
+
This class is basis to model other concepts.
|
|
51
|
+
|
|
52
|
+
Example for `prov:Person`:
|
|
53
|
+
|
|
54
|
+
>>> @namespaces(prov='http://www.w3.org/ns/prov#',
|
|
55
|
+
>>> foaf='http://xmlns.com/foaf/0.1/')
|
|
56
|
+
>>> @urirefs(Person='prov:Person', first_name='foaf:firstName')
|
|
57
|
+
>>> class Person(Thing):
|
|
58
|
+
>>> first_name: str = None
|
|
59
|
+
>>> last_name: str = None
|
|
60
|
+
|
|
61
|
+
>>> p = Person(first_name='John', last_name='Doe', age=30)
|
|
62
|
+
>>> # Note, that age is not defined in the class! This is allowed, but may not be
|
|
63
|
+
>>> # serialized into an IRI although the ontology defines it
|
|
64
|
+
|
|
65
|
+
>>> print(p.model_dump_jsonld())
|
|
66
|
+
>>> {
|
|
67
|
+
>>> "@context": {
|
|
68
|
+
>>> "prov": "http://www.w3.org/ns/prov#",
|
|
69
|
+
>>> "foaf": "http://xmlns.com/foaf/0.1/",
|
|
70
|
+
>>> "first_name": "foaf:firstName"
|
|
71
|
+
>>> },
|
|
72
|
+
>>> "@id": "N23036f1a4eb149edb7db41b2f5f4268c",
|
|
73
|
+
>>> "@type": "prov:Person",
|
|
74
|
+
>>> "foaf:firstName": "John",
|
|
75
|
+
>>> "last_name": "Doe",
|
|
76
|
+
>>> "age": "30" # Age appears as a field without context!
|
|
77
|
+
>>> }
|
|
78
|
+
|
|
79
|
+
Note, that values are validated, as `Thing` is a subclass of `pydantic.BaseModel`:
|
|
80
|
+
|
|
81
|
+
>>> Person(first_name=1)
|
|
82
|
+
|
|
83
|
+
Will lead to a validation error:
|
|
84
|
+
|
|
85
|
+
>>> # Traceback (most recent call last):
|
|
86
|
+
>>> # ...
|
|
87
|
+
>>> # pydantic_core._pydantic_core.ValidationError: 1 validation error for Person
|
|
88
|
+
>>> # first_name
|
|
89
|
+
>>> # Input should be a valid string [type=string_type, input_value=1, input_type=int]
|
|
90
|
+
>>> # For further information visit https://errors.pydantic.dev/2.4/v/string_type
|
|
91
|
+
|
|
33
92
|
"""
|
|
34
93
|
id: Union[str, HttpUrl, FileUrl, BlankNodeType, None] = None # @id
|
|
35
94
|
label: str = None # rdfs:label
|
|
@@ -46,7 +105,8 @@ class Thing(ThingModel):
|
|
|
46
105
|
|
|
47
106
|
def get_jsonld_dict(self,
|
|
48
107
|
context: Optional[Union[Dict, str]] = None,
|
|
49
|
-
exclude_none: bool = True
|
|
108
|
+
exclude_none: bool = True,
|
|
109
|
+
resolve_keys: bool = False) -> Dict:
|
|
50
110
|
"""Return the JSON-LD dictionary of the object. This will include the context
|
|
51
111
|
and the fields of the object.
|
|
52
112
|
|
|
@@ -57,6 +117,42 @@ class Thing(ThingModel):
|
|
|
57
117
|
be interpreted as an import statement and will be added to the context.
|
|
58
118
|
exclude_none: bool=True
|
|
59
119
|
Exclude fields with None values
|
|
120
|
+
resolve_keys: bool=False
|
|
121
|
+
If True, then attributes of a Thing class will be resolved to the full IRI and
|
|
122
|
+
explained in the context.
|
|
123
|
+
|
|
124
|
+
Example:
|
|
125
|
+
|
|
126
|
+
In the following example, first_name refers to foaf:firstName:
|
|
127
|
+
|
|
128
|
+
>>> @namespaces(foaf='http://xmlns.com/foaf/0.1/')
|
|
129
|
+
>>> @urirefs(Person='foaf:Person', first_name='foaf:firstName')
|
|
130
|
+
>>> class Person(Thing):
|
|
131
|
+
>>> first_name: str = None
|
|
132
|
+
|
|
133
|
+
>>> p = Person(first_name='John')
|
|
134
|
+
>>> p.model_dump_jsonld(resolve_keys=True)
|
|
135
|
+
|
|
136
|
+
This will result "first_name": "foaf:firstName" showing up in the context:
|
|
137
|
+
|
|
138
|
+
>>> {
|
|
139
|
+
>>> "@context": {
|
|
140
|
+
>>> "foaf": "http://xmlns.com/foaf/0.1/",
|
|
141
|
+
>>> "first_name": "foaf:firstName"
|
|
142
|
+
>>> },
|
|
143
|
+
>>> "@type": "foaf:Person",
|
|
144
|
+
>>> "foaf:firstName": "John"
|
|
145
|
+
>>> }
|
|
146
|
+
|
|
147
|
+
While resolve_keys=False will result in:
|
|
148
|
+
|
|
149
|
+
>>> {
|
|
150
|
+
>>> "@context": {
|
|
151
|
+
>>> "foaf": "http://xmlns.com/foaf/0.1/"
|
|
152
|
+
>>> },
|
|
153
|
+
>>> "@type": "foaf:Person",
|
|
154
|
+
>>> "foaf:firstName": "John"
|
|
155
|
+
>>> }
|
|
60
156
|
|
|
61
157
|
Returns
|
|
62
158
|
-------
|
|
@@ -121,7 +217,13 @@ class Thing(ThingModel):
|
|
|
121
217
|
if _is_http_url(iri):
|
|
122
218
|
serialized_fields[iri] = value
|
|
123
219
|
if isinstance(value, str): # this turn URLs into base strings
|
|
124
|
-
|
|
220
|
+
ns, key = split_URIRef(iri)
|
|
221
|
+
if key != k and not resolve_keys:
|
|
222
|
+
at_context[k] = resolve_iri(iri,
|
|
223
|
+
at_context) # the key is not resolved, hence the key and iri is put into context dict:
|
|
224
|
+
serialized_fields[k] = str(value)
|
|
225
|
+
else:
|
|
226
|
+
serialized_fields[iri] = str(value)
|
|
125
227
|
else:
|
|
126
228
|
serialized_fields[iri] = value
|
|
127
229
|
else:
|
|
@@ -137,13 +239,13 @@ class Thing(ThingModel):
|
|
|
137
239
|
except AttributeError as e:
|
|
138
240
|
raise AttributeError(f"Could not serialize {obj} ({obj.__class__}). Orig. err: {e}") from e
|
|
139
241
|
|
|
140
|
-
if obj.model_config['extra'] == 'allow':
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
242
|
+
# if obj.model_config['extra'] == 'allow':
|
|
243
|
+
# for k, v in obj.model_extra.items():
|
|
244
|
+
# iri = uri_ref_manager.get(k, k)
|
|
245
|
+
# if _is_http_url(iri):
|
|
246
|
+
# serialized_fields[k] = v
|
|
247
|
+
# else:
|
|
248
|
+
# serialized_fields[iri] = v
|
|
147
249
|
|
|
148
250
|
# datetime
|
|
149
251
|
for k, v in serialized_fields.copy().items():
|
|
@@ -179,7 +281,8 @@ class Thing(ThingModel):
|
|
|
179
281
|
def model_dump_jsonld(self,
|
|
180
282
|
context: Optional[Dict] = None,
|
|
181
283
|
exclude_none: bool = True,
|
|
182
|
-
rdflib_serialize: bool = False
|
|
284
|
+
rdflib_serialize: bool = False,
|
|
285
|
+
resolve_keys: bool = False) -> str:
|
|
183
286
|
"""Similar to model_dump_json() but will return a JSON string with
|
|
184
287
|
context resulting in a JSON-LD serialization. Using `rdflib_serialize=True`
|
|
185
288
|
will use the rdflib to serialize. This will make the output a bit cleaner
|
|
@@ -199,6 +302,11 @@ class Thing(ThingModel):
|
|
|
199
302
|
If True, the output will be serialized using rdflib. This results in a cleaner
|
|
200
303
|
output but is not needed in most cases and just takes a bit more time (and requires
|
|
201
304
|
an internet connection). Will also generate a blank node if no ID is set.
|
|
305
|
+
resolve_keys: bool=False
|
|
306
|
+
If True, then attributes of a Thing class will be resolved to the full IRI and
|
|
307
|
+
explained in the context.
|
|
308
|
+
|
|
309
|
+
.. seealso:: `Thing.get_jsonld_dict`
|
|
202
310
|
|
|
203
311
|
Returns
|
|
204
312
|
-------
|
|
@@ -207,7 +315,8 @@ class Thing(ThingModel):
|
|
|
207
315
|
"""
|
|
208
316
|
jsonld_dict = self.get_jsonld_dict(
|
|
209
317
|
context=context,
|
|
210
|
-
exclude_none=exclude_none
|
|
318
|
+
exclude_none=exclude_none,
|
|
319
|
+
resolve_keys=resolve_keys
|
|
211
320
|
)
|
|
212
321
|
jsonld_str = json.dumps(jsonld_dict, indent=4)
|
|
213
322
|
if not rdflib_serialize:
|
|
@@ -244,9 +353,37 @@ class Thing(ThingModel):
|
|
|
244
353
|
return f"{self.__class__.__name__}({repr_fields})"
|
|
245
354
|
|
|
246
355
|
@classmethod
|
|
247
|
-
def from_jsonld(cls,
|
|
248
|
-
|
|
356
|
+
def from_jsonld(cls,
|
|
357
|
+
source: Optional[Union[str, pathlib.Path]] = None,
|
|
358
|
+
data: Optional[Dict] = None,
|
|
359
|
+
limit: Optional[int] = None,
|
|
360
|
+
context: Optional[Dict] = None):
|
|
361
|
+
"""Initialize the class from a JSON-LD source
|
|
362
|
+
|
|
363
|
+
Note the inconsistency in the schema.org protocol. Codemeta for instance uses http whereas
|
|
364
|
+
https is the current standard. This repo only works with https. If you have a http schema,
|
|
365
|
+
this method will replace http with https.
|
|
366
|
+
|
|
367
|
+
Parameters
|
|
368
|
+
----------
|
|
369
|
+
source: Optional[Union[str, pathlib.Path]]=None
|
|
370
|
+
The source of the JSON-LD data (filename). Must be given if data is None.
|
|
371
|
+
data: Optional[str]=None
|
|
372
|
+
The JSON-LD data as a string. Must be given if source is None.
|
|
373
|
+
limit: Optional[int]=None
|
|
374
|
+
The limit of the number of objects to return. If None, all objects will be returned.
|
|
375
|
+
If limit is 1, then the first object will be returned, else a list of objects.
|
|
376
|
+
context: Optional[Dict]=None
|
|
377
|
+
The context to use for the JSON-LD serialization. If a string is given, it will
|
|
378
|
+
be interpreted as an import statement and will be added to the context.
|
|
379
|
+
|
|
380
|
+
"""
|
|
249
381
|
from . import query
|
|
382
|
+
if data is not None and isinstance(data, str):
|
|
383
|
+
if 'http://schema.org/' in data:
|
|
384
|
+
print(
|
|
385
|
+
'Replacing http with https in the JSON-LD data. This is a workaround for the schema.org inconsistency.')
|
|
386
|
+
data = data.replace('http://schema.org/', 'https://schema.org/')
|
|
250
387
|
return query(cls, source=source, data=data, limit=limit, context=context)
|
|
251
388
|
|
|
252
389
|
@classmethod
|
|
@@ -277,17 +414,27 @@ class Thing(ThingModel):
|
|
|
277
414
|
ns_iri = NamespaceManager[cls].get(ns, None)
|
|
278
415
|
return f'{ns_iri}{key}'
|
|
279
416
|
|
|
280
|
-
@classmethod
|
|
281
|
-
def get_context(cls):
|
|
282
|
-
"""Return the context of the class"""
|
|
283
|
-
return NamespaceManager[cls]
|
|
284
|
-
|
|
285
417
|
@property
|
|
286
|
-
def namespaces(self):
|
|
418
|
+
def namespaces(self) -> Dict:
|
|
287
419
|
"""Return the namespaces of the class"""
|
|
288
|
-
return
|
|
420
|
+
return get_namespaces(self.__class__)
|
|
289
421
|
|
|
290
422
|
@property
|
|
291
|
-
def urirefs(self):
|
|
292
|
-
"""Return the
|
|
293
|
-
return
|
|
423
|
+
def urirefs(self) -> Dict:
|
|
424
|
+
"""Return the urirefs of the class"""
|
|
425
|
+
return get_urirefs(self.__class__)
|
|
426
|
+
|
|
427
|
+
@classmethod
|
|
428
|
+
def get_context(cls) -> Dict:
|
|
429
|
+
"""Return the context of the class"""
|
|
430
|
+
return get_namespaces(cls)
|
|
431
|
+
|
|
432
|
+
|
|
433
|
+
def get_urirefs(cls: Thing) -> Dict:
|
|
434
|
+
"""Return the URIRefs of the class"""
|
|
435
|
+
return URIRefManager[cls]
|
|
436
|
+
|
|
437
|
+
|
|
438
|
+
def get_namespaces(cls: Thing) -> Dict:
|
|
439
|
+
"""Return the namespaces of the class"""
|
|
440
|
+
return NamespaceManager[cls]
|
|
@@ -4,7 +4,7 @@ import pydantic
|
|
|
4
4
|
import unittest
|
|
5
5
|
from pydantic import EmailStr
|
|
6
6
|
|
|
7
|
-
from ontolutils import Thing, urirefs, namespaces
|
|
7
|
+
from ontolutils import Thing, urirefs, namespaces, get_urirefs, get_namespaces
|
|
8
8
|
from ontolutils import set_logging_level
|
|
9
9
|
from ontolutils.classes import decorator
|
|
10
10
|
|
|
@@ -14,6 +14,7 @@ LOG_LEVEL = logging.DEBUG
|
|
|
14
14
|
class TestNamespaces(unittest.TestCase):
|
|
15
15
|
|
|
16
16
|
def setUp(self):
|
|
17
|
+
self.maxDiff = None
|
|
17
18
|
logger = logging.getLogger('ontolutils')
|
|
18
19
|
self.INITIAL_LOG_LEVEL = logger.level
|
|
19
20
|
|
|
@@ -25,6 +26,22 @@ class TestNamespaces(unittest.TestCase):
|
|
|
25
26
|
set_logging_level(self.INITIAL_LOG_LEVEL)
|
|
26
27
|
assert logging.getLogger('ontolutils').level == self.INITIAL_LOG_LEVEL
|
|
27
28
|
|
|
29
|
+
def test_thing_custom_prop(self):
|
|
30
|
+
"""It is helpful to have the properties equal to the urirefs keys,
|
|
31
|
+
however, this should not be required!"""
|
|
32
|
+
|
|
33
|
+
@namespaces(foaf='http://xmlns.com/foaf/0.1/',
|
|
34
|
+
prov='http://www.w3.org/ns/prov#')
|
|
35
|
+
@urirefs(Person='prov:Person',
|
|
36
|
+
first_name='foaf:firstName',
|
|
37
|
+
lastName='foaf:lastName')
|
|
38
|
+
class Person(Thing):
|
|
39
|
+
first_name: str = None
|
|
40
|
+
lastName: str
|
|
41
|
+
|
|
42
|
+
p = Person(first_name='John', lastName='Doe')
|
|
43
|
+
print(p.model_dump_jsonld(resolve_keys=False))
|
|
44
|
+
|
|
28
45
|
def test_sort_classes(self):
|
|
29
46
|
thing1 = Thing(label='Thing 1')
|
|
30
47
|
thing2 = Thing(label='Thing 2')
|
|
@@ -255,15 +272,19 @@ class TestNamespaces(unittest.TestCase):
|
|
|
255
272
|
pass
|
|
256
273
|
|
|
257
274
|
mt = CustomPerson()
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
275
|
+
# custom person has no
|
|
276
|
+
self.assertDictEqual(mt.urirefs, get_urirefs(Thing))
|
|
277
|
+
self.assertDictEqual(mt.urirefs, {'Thing': 'owl:Thing', 'label': 'rdfs:label'})
|
|
278
|
+
self.assertDictEqual(mt.namespaces, get_namespaces(Thing))
|
|
279
|
+
self.assertDictEqual(mt.namespaces, {'owl': 'http://www.w3.org/2002/07/owl#',
|
|
280
|
+
'rdfs': 'http://www.w3.org/2000/01/rdf-schema#'})
|
|
261
281
|
|
|
262
282
|
mt = CustomPerson(first_name='John', last_name='Doe')
|
|
263
283
|
with self.assertRaises(AttributeError):
|
|
264
284
|
mt.namespaces = 'http://xmlns.com/foaf/0.1/'
|
|
265
285
|
with self.assertRaises(AttributeError):
|
|
266
286
|
mt.urirefs = 'foaf:lastName'
|
|
287
|
+
|
|
267
288
|
mt.namespaces['foaf'] = 'http://xmlns.com/foaf/0.1/'
|
|
268
289
|
mt.urirefs['first_name'] = 'foaf:firstName'
|
|
269
290
|
mt.urirefs['last_name'] = 'foaf:lastName'
|
|
@@ -276,10 +297,26 @@ class TestNamespaces(unittest.TestCase):
|
|
|
276
297
|
},
|
|
277
298
|
"@type": "CustomPerson",
|
|
278
299
|
"foaf:firstName": "John",
|
|
279
|
-
"foaf:lastName": "Doe"
|
|
280
|
-
|
|
300
|
+
"foaf:lastName": "Doe"
|
|
301
|
+
}
|
|
302
|
+
jsonld_dict = json.loads(mt.model_dump_jsonld(resolve_keys=True))
|
|
303
|
+
jsonld_dict.pop('@id')
|
|
304
|
+
self.assertDictEqual(jsonld_dict,
|
|
305
|
+
ref_jsonld)
|
|
306
|
+
|
|
307
|
+
ref_jsonld = {
|
|
308
|
+
"@context": {
|
|
309
|
+
"owl": "http://www.w3.org/2002/07/owl#",
|
|
310
|
+
"rdfs": "http://www.w3.org/2000/01/rdf-schema#",
|
|
311
|
+
"foaf": "http://xmlns.com/foaf/0.1/",
|
|
312
|
+
"first_name": "http://xmlns.com/foaf/0.1/firstName",
|
|
313
|
+
"last_name": "http://xmlns.com/foaf/0.1/lastName"
|
|
314
|
+
},
|
|
315
|
+
"@type": "CustomPerson",
|
|
316
|
+
"first_name": "John",
|
|
317
|
+
"last_name": "Doe"
|
|
281
318
|
}
|
|
282
|
-
jsonld_dict = json.loads(mt.model_dump_jsonld())
|
|
319
|
+
jsonld_dict = json.loads(mt.model_dump_jsonld(resolve_keys=False))
|
|
283
320
|
jsonld_dict.pop('@id')
|
|
284
321
|
self.assertDictEqual(jsonld_dict,
|
|
285
322
|
ref_jsonld)
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
"""Testing code used in the README.md file"""
|
|
2
2
|
import pathlib
|
|
3
|
-
|
|
4
3
|
from pydantic import EmailStr
|
|
5
4
|
|
|
6
5
|
from ontolutils import Thing, namespaces, urirefs
|
|
@@ -38,3 +37,17 @@ found_agent = found_agents[0]
|
|
|
38
37
|
print(found_agent.mbox)
|
|
39
38
|
|
|
40
39
|
pathlib.Path("agent.json").unlink(missing_ok=True)
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
@namespaces(prov='http://www.w3.org/ns/prov#',
|
|
43
|
+
|
|
44
|
+
foaf='http://xmlns.com/foaf/0.1/')
|
|
45
|
+
@urirefs(Person='prov:Person', first_name='foaf:firstName')
|
|
46
|
+
class Person(Thing):
|
|
47
|
+
first_name: str = None
|
|
48
|
+
last_name: str = None
|
|
49
|
+
|
|
50
|
+
p = Person(first_name='John', last_name='Doe', age=30)
|
|
51
|
+
print(p.model_dump_jsonld())
|
|
52
|
+
|
|
53
|
+
# Person(first_name=1)
|
|
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
|