ontolutils 0.6.2__tar.gz → 0.7.0__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.
Files changed (40) hide show
  1. {ontolutils-0.6.2 → ontolutils-0.7.0}/PKG-INFO +10 -5
  2. {ontolutils-0.6.2 → ontolutils-0.7.0}/README.md +9 -4
  3. {ontolutils-0.6.2 → ontolutils-0.7.0}/ontolutils/__init__.py +3 -2
  4. {ontolutils-0.6.2 → ontolutils-0.7.0}/ontolutils/classes/__init__.py +2 -1
  5. {ontolutils-0.6.2 → ontolutils-0.7.0}/ontolutils/classes/thing.py +8 -1
  6. {ontolutils-0.6.2 → ontolutils-0.7.0}/ontolutils/classes/utils.py +16 -0
  7. {ontolutils-0.6.2 → ontolutils-0.7.0}/ontolutils.egg-info/PKG-INFO +10 -5
  8. {ontolutils-0.6.2 → ontolutils-0.7.0}/setup.cfg +1 -1
  9. {ontolutils-0.6.2 → ontolutils-0.7.0}/tests/test_classes.py +110 -4
  10. {ontolutils-0.6.2 → ontolutils-0.7.0}/LICENSE +0 -0
  11. {ontolutils-0.6.2 → ontolutils-0.7.0}/ontolutils/_version.py +0 -0
  12. {ontolutils-0.6.2 → ontolutils-0.7.0}/ontolutils/cache.py +0 -0
  13. {ontolutils-0.6.2 → ontolutils-0.7.0}/ontolutils/classes/decorator.py +0 -0
  14. {ontolutils-0.6.2 → ontolutils-0.7.0}/ontolutils/classes/query_util.py +0 -0
  15. {ontolutils-0.6.2 → ontolutils-0.7.0}/ontolutils/classes/typing.py +0 -0
  16. {ontolutils-0.6.2 → ontolutils-0.7.0}/ontolutils/config.py +0 -0
  17. {ontolutils-0.6.2 → ontolutils-0.7.0}/ontolutils/namespacelib/__dev.py +0 -0
  18. {ontolutils-0.6.2 → ontolutils-0.7.0}/ontolutils/namespacelib/__init__.py +0 -0
  19. {ontolutils-0.6.2 → ontolutils-0.7.0}/ontolutils/namespacelib/_iana_utils.py +0 -0
  20. {ontolutils-0.6.2 → ontolutils-0.7.0}/ontolutils/namespacelib/codemeta.py +0 -0
  21. {ontolutils-0.6.2 → ontolutils-0.7.0}/ontolutils/namespacelib/m4i.py +0 -0
  22. {ontolutils-0.6.2 → ontolutils-0.7.0}/ontolutils/namespacelib/obo.py +0 -0
  23. {ontolutils-0.6.2 → ontolutils-0.7.0}/ontolutils/namespacelib/qudt_kind.py +0 -0
  24. {ontolutils-0.6.2 → ontolutils-0.7.0}/ontolutils/namespacelib/qudt_unit.py +0 -0
  25. {ontolutils-0.6.2 → ontolutils-0.7.0}/ontolutils/namespacelib/schema.py +0 -0
  26. {ontolutils-0.6.2 → ontolutils-0.7.0}/ontolutils/utils/__init__.py +0 -0
  27. {ontolutils-0.6.2 → ontolutils-0.7.0}/ontolutils/utils/qudt_units.py +0 -0
  28. {ontolutils-0.6.2 → ontolutils-0.7.0}/ontolutils.egg-info/SOURCES.txt +0 -0
  29. {ontolutils-0.6.2 → ontolutils-0.7.0}/ontolutils.egg-info/dependency_links.txt +0 -0
  30. {ontolutils-0.6.2 → ontolutils-0.7.0}/ontolutils.egg-info/requires.txt +0 -0
  31. {ontolutils-0.6.2 → ontolutils-0.7.0}/ontolutils.egg-info/top_level.txt +0 -0
  32. {ontolutils-0.6.2 → ontolutils-0.7.0}/pyproject.toml +0 -0
  33. {ontolutils-0.6.2 → ontolutils-0.7.0}/setup.py +0 -0
  34. {ontolutils-0.6.2 → ontolutils-0.7.0}/tests/test_decorator.py +0 -0
  35. {ontolutils-0.6.2 → ontolutils-0.7.0}/tests/test_namespaces.py +0 -0
  36. {ontolutils-0.6.2 → ontolutils-0.7.0}/tests/test_query.py +0 -0
  37. {ontolutils-0.6.2 → ontolutils-0.7.0}/tests/test_readme.py +0 -0
  38. {ontolutils-0.6.2 → ontolutils-0.7.0}/tests/test_typing.py +0 -0
  39. {ontolutils-0.6.2 → ontolutils-0.7.0}/tests/test_utils.py +0 -0
  40. {ontolutils-0.6.2 → ontolutils-0.7.0}/tests/test_version.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: ontolutils
3
- Version: 0.6.2
3
+ Version: 0.7.0
4
4
  Summary: Utility library for the work with ontologies.
5
5
  Home-page: https://github.com/matthiasprobst/ontology-utils
6
6
  Author: Matthias Probst
@@ -57,9 +57,10 @@ lets you design classes, which describe ontology classes like this:
57
57
 
58
58
  ```python
59
59
  from pydantic import EmailStr, Field
60
+ from pydantic import HttpUrl, model_validator
61
+
62
+ from ontolutils import Thing, urirefs, namespaces, as_id
60
63
 
61
- from ontolutils import Thing, urirefs, namespaces
62
- from pydantic import HttpUrl
63
64
 
64
65
  @namespaces(prov="http://www.w3.org/ns/prov#",
65
66
  foaf="http://xmlns.com/foaf/0.1/")
@@ -71,8 +72,12 @@ class Person(Thing):
71
72
  firstName: str
72
73
  last_name: str = Field(default=None, alias="lastName") # you may provide an alias
73
74
  mbox: EmailStr = None
74
- orcidId: HttpUrl = Field(default=None, alias="foaf:orcidId",
75
- json_schema_extra={'use_as_id': True}) # use this as ID if available
75
+ orcidId: HttpUrl = Field(default=None, alias="foaf:orcidId")
76
+
77
+ # the following will ensure, that if orcidId is set, it will be used as the id
78
+ @model_validator(mode="before")
79
+ def _change_id(self):
80
+ return as_id(self, "orcidId")
76
81
 
77
82
 
78
83
  p = Person(id="https://orcid.org/0000-0001-8729-0482",
@@ -25,9 +25,10 @@ lets you design classes, which describe ontology classes like this:
25
25
 
26
26
  ```python
27
27
  from pydantic import EmailStr, Field
28
+ from pydantic import HttpUrl, model_validator
29
+
30
+ from ontolutils import Thing, urirefs, namespaces, as_id
28
31
 
29
- from ontolutils import Thing, urirefs, namespaces
30
- from pydantic import HttpUrl
31
32
 
32
33
  @namespaces(prov="http://www.w3.org/ns/prov#",
33
34
  foaf="http://xmlns.com/foaf/0.1/")
@@ -39,8 +40,12 @@ class Person(Thing):
39
40
  firstName: str
40
41
  last_name: str = Field(default=None, alias="lastName") # you may provide an alias
41
42
  mbox: EmailStr = None
42
- orcidId: HttpUrl = Field(default=None, alias="foaf:orcidId",
43
- json_schema_extra={'use_as_id': True}) # use this as ID if available
43
+ orcidId: HttpUrl = Field(default=None, alias="foaf:orcidId")
44
+
45
+ # the following will ensure, that if orcidId is set, it will be used as the id
46
+ @model_validator(mode="before")
47
+ def _change_id(self):
48
+ return as_id(self, "orcidId")
44
49
 
45
50
 
46
51
  p = Person(id="https://orcid.org/0000-0001-8729-0482",
@@ -2,7 +2,7 @@
2
2
  import logging
3
3
 
4
4
  from ._version import __version__
5
- from .classes import Thing, get_urirefs, get_namespaces
5
+ from .classes import Thing, get_urirefs, get_namespaces, as_id
6
6
  from .classes import namespaces, urirefs
7
7
  from .classes import query, dquery
8
8
  from .classes.utils import merge_jsonld
@@ -39,5 +39,6 @@ __all__ = ['Thing',
39
39
  'dquery',
40
40
  'get_urirefs',
41
41
  'get_namespaces',
42
- 'parse_unit'
42
+ 'parse_unit',
43
+ 'as_id'
43
44
  ]
@@ -2,5 +2,6 @@
2
2
  from .decorator import namespaces, urirefs
3
3
  from .query_util import query, dquery
4
4
  from .thing import Thing, get_urirefs, get_namespaces
5
+ from .utils import as_id
5
6
 
6
- __all__ = ['namespaces', 'urirefs', 'Thing', 'query', 'dquery', 'get_urirefs', 'get_namespaces']
7
+ __all__ = ['namespaces', 'urirefs', 'Thing', 'query', 'dquery', 'get_urirefs', 'get_namespaces', 'as_id']
@@ -291,9 +291,16 @@ class Thing(ThingModel):
291
291
  "@context": at_context,
292
292
  **serialization
293
293
  }
294
- for field_name, field_value in self.__class__.model_json_schema()["properties"].items():
294
+
295
+ properties = self.__class__.model_json_schema().get("properties", {})
296
+ if not properties:
297
+ properties = self.__class__.model_json_schema().get("items", {}).get(self.__class__.__name__, {}).get(
298
+ "properties", {})
299
+
300
+ for field_name, field_value in properties.items():
295
301
  _use_as_id = field_value.get("use_as_id", None)
296
302
  if _use_as_id is not None:
303
+ warnings.warn("The use_as_id field is deprecated. Use the @id field instead.", DeprecationWarning)
297
304
  _id = getattr(self, field_name)
298
305
  if _id is not None:
299
306
  if str(_id).startswith(("_:", "http")):
@@ -147,3 +147,19 @@ def download_file(url,
147
147
  f.write(content)
148
148
 
149
149
  return dest_filename
150
+
151
+
152
+ def as_id(obj, field_name):
153
+ if isinstance(obj, dict):
154
+ return as_id_before(obj, field_name)
155
+ raise ValueError(f"You must use mode='before' for as_id")
156
+
157
+
158
+ def as_id_before(obj: Dict, field_name: str):
159
+ if obj.get(field_name, None) is not None:
160
+ _id_url = obj[field_name]
161
+ if not str(_id_url).startswith(("_:", "http")):
162
+ raise ValueError(f"Field {field_name} is not a URIRef or BNode: {obj[field_name]}. "
163
+ f"You can only set an id for URIRefs or BNodes.")
164
+ obj["id"] = _id_url
165
+ return obj
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: ontolutils
3
- Version: 0.6.2
3
+ Version: 0.7.0
4
4
  Summary: Utility library for the work with ontologies.
5
5
  Home-page: https://github.com/matthiasprobst/ontology-utils
6
6
  Author: Matthias Probst
@@ -57,9 +57,10 @@ lets you design classes, which describe ontology classes like this:
57
57
 
58
58
  ```python
59
59
  from pydantic import EmailStr, Field
60
+ from pydantic import HttpUrl, model_validator
61
+
62
+ from ontolutils import Thing, urirefs, namespaces, as_id
60
63
 
61
- from ontolutils import Thing, urirefs, namespaces
62
- from pydantic import HttpUrl
63
64
 
64
65
  @namespaces(prov="http://www.w3.org/ns/prov#",
65
66
  foaf="http://xmlns.com/foaf/0.1/")
@@ -71,8 +72,12 @@ class Person(Thing):
71
72
  firstName: str
72
73
  last_name: str = Field(default=None, alias="lastName") # you may provide an alias
73
74
  mbox: EmailStr = None
74
- orcidId: HttpUrl = Field(default=None, alias="foaf:orcidId",
75
- json_schema_extra={'use_as_id': True}) # use this as ID if available
75
+ orcidId: HttpUrl = Field(default=None, alias="foaf:orcidId")
76
+
77
+ # the following will ensure, that if orcidId is set, it will be used as the id
78
+ @model_validator(mode="before")
79
+ def _change_id(self):
80
+ return as_id(self, "orcidId")
76
81
 
77
82
 
78
83
  p = Person(id="https://orcid.org/0000-0001-8729-0482",
@@ -1,6 +1,6 @@
1
1
  [metadata]
2
2
  name = ontolutils
3
- version = 0.6.2
3
+ version = 0.7.0
4
4
  author = Matthias Probst
5
5
  author_email = matthias.probst@kit.edu
6
6
  description = Utility library for the work with ontologies.
@@ -6,11 +6,12 @@ from typing import Optional
6
6
 
7
7
  import pydantic
8
8
  import rdflib
9
- from pydantic import EmailStr
9
+ from pydantic import EmailStr, model_validator
10
10
  from pydantic import field_validator, Field
11
11
  from rdflib.plugins.shared.jsonld.context import Context
12
12
 
13
13
  from ontolutils import Thing, urirefs, namespaces, get_urirefs, get_namespaces
14
+ from ontolutils import as_id
14
15
  from ontolutils import set_logging_level
15
16
  from ontolutils.classes import decorator
16
17
  from ontolutils.classes.thing import resolve_iri
@@ -429,14 +430,13 @@ class TestNamespaces(unittest.TestCase):
429
430
  self.assertEqual(Agent.iri('mbox'), 'http://xmlns.com/foaf/0.1/mbox')
430
431
  self.assertEqual(Agent.iri('mbox', compact=True), 'foaf:mbox')
431
432
 
432
- def test_person(self):
433
+ def test_use_as_id(self):
433
434
  @namespaces(prov="http://www.w3.org/ns/prov#",
434
435
  foaf="http://xmlns.com/foaf/0.1/",
435
436
  m4i="http://w3id.org/nfdi4ing/metadata4ing#"
436
437
  )
437
438
  @urirefs(Person='prov:Person',
438
439
  firstName='foaf:firstName',
439
- # lastName=FOAF.lastName,
440
440
  lastName='foaf:lastName',
441
441
  orcidId='m4i:orcidId',
442
442
  mbox='foaf:mbox')
@@ -444,7 +444,11 @@ class TestNamespaces(unittest.TestCase):
444
444
  firstName: str
445
445
  lastName: str = None
446
446
  mbox: EmailStr = None
447
- orcidId: str = Field(default=None, alias="orcid_id", json_schema_extra={'use_as_id': True})
447
+ orcidId: str = Field(default=None, alias="orcid_id")
448
+
449
+ @model_validator(mode="before")
450
+ def _change_id(self):
451
+ return as_id(self, "orcidId")
448
452
 
449
453
  p = Person(
450
454
  id="local:cde4c79c-21f2-4ab7-b01d-28de6e4aade4",
@@ -469,6 +473,108 @@ class TestNamespaces(unittest.TestCase):
469
473
  self.assertDictEqual(json.loads(p.model_dump_jsonld()),
470
474
  jsonld)
471
475
 
476
+ def test_use_as_id_V2(self):
477
+ @namespaces(schema="https://schema.org/",
478
+ foaf="http://xmlns.com/foaf/0.1/",
479
+ )
480
+ @urirefs(Orga='prov:Organization',
481
+ name="schema:name",
482
+ identifier="schema:identifier",
483
+ mbox='foaf:mbox')
484
+ class Orga(Thing):
485
+ identifier: str = Field(default=None, alias="identifier")
486
+ name: str = Field(default=None, alias="name")
487
+ mbox: EmailStr = None
488
+
489
+ @model_validator(mode="before")
490
+ def _change_id(self):
491
+ return as_id(self, "identifier")
492
+
493
+ @namespaces(prov="http://www.w3.org/ns/prov#",
494
+ foaf="http://xmlns.com/foaf/0.1/",
495
+ m4i="http://w3id.org/nfdi4ing/metadata4ing#"
496
+ )
497
+ @urirefs(Person='prov:Person',
498
+ firstName='foaf:firstName',
499
+ lastName='foaf:lastName',
500
+ orcidId='m4i:orcidId',
501
+ mbox='foaf:mbox')
502
+ class Person(Thing):
503
+ firstName: str
504
+ lastName: str = None
505
+ mbox: EmailStr = None
506
+ orcidId: str = Field(default=None, alias="orcid_id")
507
+ affiliation: Orga = None
508
+
509
+ @model_validator(mode="before")
510
+ def _change_id(self):
511
+ return as_id(self, "orcidId")
512
+
513
+ with self.assertRaises(ValueError):
514
+ p = Person(
515
+ id="local:cde4c79c-21f2-4ab7-b01d-28de6e4aade4",
516
+ firstName='John',
517
+ lastName='Doe',
518
+ orcidId='https://orcid.org/0000-0001-8729-0482',
519
+ affiliation=Orga(identifier='123', name='Orga 1')
520
+ )
521
+ p = Person(
522
+ id="local:cde4c79c-21f2-4ab7-b01d-28de6e4aade4",
523
+ firstName='John',
524
+ lastName='Doe',
525
+ orcidId='https://orcid.org/0000-0001-8729-0482',
526
+ affiliation=Orga(identifier='https://example.org/123', name='Orga 1')
527
+ )
528
+ pdict = json.loads(p.model_dump_jsonld())
529
+ assert pdict['@id'] == 'https://orcid.org/0000-0001-8729-0482'
530
+ assert pdict['affiliation']["@id"] == 'https://example.org/123'
531
+
532
+ def test_use_as_id_V3(self):
533
+ @namespaces(schema="https://schema.org/",
534
+ foaf="http://xmlns.com/foaf/0.1/",
535
+ )
536
+ @urirefs(Orga='prov:Organization',
537
+ name="schema:name",
538
+ identifier="schema:identifier",
539
+ mbox='foaf:mbox')
540
+ class Orga(Thing):
541
+ identifier: str = Field(default=None, alias="identifier")
542
+ name: str = Field(default=None, alias="name")
543
+ mbox: EmailStr = None
544
+
545
+ @model_validator(mode="after")
546
+ def _change_id(self):
547
+ return as_id(self, "identifier")
548
+
549
+ @namespaces(prov="http://www.w3.org/ns/prov#",
550
+ foaf="http://xmlns.com/foaf/0.1/",
551
+ m4i="http://w3id.org/nfdi4ing/metadata4ing#"
552
+ )
553
+ @urirefs(Person='prov:Person',
554
+ firstName='foaf:firstName',
555
+ lastName='foaf:lastName',
556
+ orcidId='m4i:orcidId',
557
+ mbox='foaf:mbox')
558
+ class Person(Thing):
559
+ firstName: str
560
+ lastName: str = None
561
+ mbox: EmailStr = None
562
+ orcidId: str = Field(default=None, alias="orcid_id")
563
+ affiliation: Orga = None
564
+
565
+ @model_validator(mode="before")
566
+ def _change_id(self):
567
+ return as_id(self, "orcidId")
568
+
569
+ with self.assertRaises(ValueError):
570
+ Person(
571
+ id="local:cde4c79c-21f2-4ab7-b01d-28de6e4aade4",
572
+ firstName='John',
573
+ lastName='Doe',
574
+ orcidId='https://orcid.org/0000-0001-8729-0482',
575
+ affiliation=Orga(identifier='123', name='Orga 1')
576
+ )
577
+
472
578
  def test_update_namespace_and_uri(self):
473
579
  class CustomPerson(Thing):
474
580
  pass
File without changes
File without changes
File without changes