ontolutils 0.2.23__tar.gz → 0.4.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.2.23 → ontolutils-0.4.0}/PKG-INFO +1 -1
  2. {ontolutils-0.2.23 → ontolutils-0.4.0}/ontolutils/classes/thing.py +12 -14
  3. {ontolutils-0.2.23 → ontolutils-0.4.0}/ontolutils.egg-info/PKG-INFO +1 -1
  4. {ontolutils-0.2.23 → ontolutils-0.4.0}/setup.cfg +1 -1
  5. {ontolutils-0.2.23 → ontolutils-0.4.0}/tests/test_classes.py +33 -13
  6. {ontolutils-0.2.23 → ontolutils-0.4.0}/LICENSE +0 -0
  7. {ontolutils-0.2.23 → ontolutils-0.4.0}/README.md +0 -0
  8. {ontolutils-0.2.23 → ontolutils-0.4.0}/ontolutils/__init__.py +0 -0
  9. {ontolutils-0.2.23 → ontolutils-0.4.0}/ontolutils/_version.py +0 -0
  10. {ontolutils-0.2.23 → ontolutils-0.4.0}/ontolutils/cache.py +0 -0
  11. {ontolutils-0.2.23 → ontolutils-0.4.0}/ontolutils/classes/__init__.py +0 -0
  12. {ontolutils-0.2.23 → ontolutils-0.4.0}/ontolutils/classes/decorator.py +0 -0
  13. {ontolutils-0.2.23 → ontolutils-0.4.0}/ontolutils/classes/query_util.py +0 -0
  14. {ontolutils-0.2.23 → ontolutils-0.4.0}/ontolutils/classes/typing.py +0 -0
  15. {ontolutils-0.2.23 → ontolutils-0.4.0}/ontolutils/classes/utils.py +0 -0
  16. {ontolutils-0.2.23 → ontolutils-0.4.0}/ontolutils/namespacelib/__dev.py +0 -0
  17. {ontolutils-0.2.23 → ontolutils-0.4.0}/ontolutils/namespacelib/__init__.py +0 -0
  18. {ontolutils-0.2.23 → ontolutils-0.4.0}/ontolutils/namespacelib/_iana_utils.py +0 -0
  19. {ontolutils-0.2.23 → ontolutils-0.4.0}/ontolutils/namespacelib/codemeta.py +0 -0
  20. {ontolutils-0.2.23 → ontolutils-0.4.0}/ontolutils/namespacelib/m4i.py +0 -0
  21. {ontolutils-0.2.23 → ontolutils-0.4.0}/ontolutils/namespacelib/obo.py +0 -0
  22. {ontolutils-0.2.23 → ontolutils-0.4.0}/ontolutils/namespacelib/qudt_kind.py +0 -0
  23. {ontolutils-0.2.23 → ontolutils-0.4.0}/ontolutils/namespacelib/qudt_unit.py +0 -0
  24. {ontolutils-0.2.23 → ontolutils-0.4.0}/ontolutils/namespacelib/schema.py +0 -0
  25. {ontolutils-0.2.23 → ontolutils-0.4.0}/ontolutils/namespacelib/ssno.py +0 -0
  26. {ontolutils-0.2.23 → ontolutils-0.4.0}/ontolutils/utils/__init__.py +0 -0
  27. {ontolutils-0.2.23 → ontolutils-0.4.0}/ontolutils/utils/qudt_units.py +0 -0
  28. {ontolutils-0.2.23 → ontolutils-0.4.0}/ontolutils.egg-info/SOURCES.txt +0 -0
  29. {ontolutils-0.2.23 → ontolutils-0.4.0}/ontolutils.egg-info/dependency_links.txt +0 -0
  30. {ontolutils-0.2.23 → ontolutils-0.4.0}/ontolutils.egg-info/requires.txt +0 -0
  31. {ontolutils-0.2.23 → ontolutils-0.4.0}/ontolutils.egg-info/top_level.txt +0 -0
  32. {ontolutils-0.2.23 → ontolutils-0.4.0}/pyproject.toml +0 -0
  33. {ontolutils-0.2.23 → ontolutils-0.4.0}/setup.py +0 -0
  34. {ontolutils-0.2.23 → ontolutils-0.4.0}/tests/test_decorator.py +0 -0
  35. {ontolutils-0.2.23 → ontolutils-0.4.0}/tests/test_namespaces.py +0 -0
  36. {ontolutils-0.2.23 → ontolutils-0.4.0}/tests/test_query.py +0 -0
  37. {ontolutils-0.2.23 → ontolutils-0.4.0}/tests/test_readme.py +0 -0
  38. {ontolutils-0.2.23 → ontolutils-0.4.0}/tests/test_typing.py +0 -0
  39. {ontolutils-0.2.23 → ontolutils-0.4.0}/tests/test_utils.py +0 -0
  40. {ontolutils-0.2.23 → ontolutils-0.4.0}/tests/test_version.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: ontolutils
3
- Version: 0.2.23
3
+ Version: 0.4.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
@@ -7,7 +7,7 @@ from datetime import datetime
7
7
  from typing import Dict, Union, Optional
8
8
 
9
9
  import rdflib
10
- from pydantic import HttpUrl, FileUrl, BaseModel, ConfigDict
10
+ from pydantic import HttpUrl, FileUrl, BaseModel, ConfigDict, field_validator, Field
11
11
  from rdflib.plugins.shared.jsonld.context import Context
12
12
 
13
13
  from .decorator import urirefs, namespaces, URIRefManager, NamespaceManager, _is_http_url
@@ -19,7 +19,7 @@ logger = logging.getLogger('ontolutils')
19
19
  EXTRA = 'allow' # or 'forbid' or 'ignore'
20
20
 
21
21
 
22
- class ThingModel(BaseModel, abc.ABC):
22
+ class ThingModel(BaseModel, abc.ABC, validate_assignment=True):
23
23
  """Abstract base model class to be used by model classes used within ontolutils"""
24
24
 
25
25
  model_config = ConfigDict(extra=EXTRA, populate_by_name=True)
@@ -44,7 +44,8 @@ def resolve_iri(key_or_iri: str, context: Context) -> Optional[str]:
44
44
  return 'http://www.w3.org/2000/01/rdf-schema#label'
45
45
  return
46
46
 
47
-
47
+ def build_blank_n3():
48
+ return rdflib.BNode().n3()
48
49
  @namespaces(owl='http://www.w3.org/2002/07/owl#',
49
50
  rdfs='http://www.w3.org/2000/01/rdf-schema#')
50
51
  @urirefs(Thing='owl:Thing', label='rdfs:label')
@@ -94,9 +95,16 @@ class Thing(ThingModel):
94
95
  >>> # For further information visit https://errors.pydantic.dev/2.4/v/string_type
95
96
 
96
97
  """
97
- id: Union[str, HttpUrl, FileUrl, BlankNodeType, None] = None # @id
98
+ id: Optional[Union[str, HttpUrl, FileUrl, BlankNodeType, None]] = Field(default_factory=build_blank_n3) # @id
98
99
  label: str = None # rdfs:label
99
100
 
101
+ @field_validator('id')
102
+ @classmethod
103
+ def _id(cls, id: Optional[Union[str, HttpUrl, FileUrl, BlankNodeType]]) -> str:
104
+ if id is None:
105
+ return rdflib.BNode().n3()
106
+ return str(id)
107
+
100
108
  def __lt__(self, other: ThingModel) -> bool:
101
109
  """Less than comparison. Useful to sort Thing objects.
102
110
  Comparison can only be done with other Thing objects and if an ID is given.
@@ -107,16 +115,6 @@ class Thing(ThingModel):
107
115
  return False
108
116
  return self.id <= other.id
109
117
 
110
- def pop_blank_node_id(self) -> Union[None, str]:
111
- """Remove the ID if it is blank node"""
112
- if self.id is None:
113
- return
114
- if self.id.startswith('http'):
115
- return
116
- _id = self.id
117
- self.id = None
118
- return _id
119
-
120
118
  def get_jsonld_dict(self,
121
119
  context: Optional[Union[Dict, str]] = None,
122
120
  exclude_none: bool = True,
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: ontolutils
3
- Version: 0.2.23
3
+ Version: 0.4.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
@@ -1,6 +1,6 @@
1
1
  [metadata]
2
2
  name = ontolutils
3
- version = 0.2.23
3
+ version = 0.4.0
4
4
  author = Matthias Probst
5
5
  author_email = matthias.probst@kit.edu
6
6
  description = Utility library for the work with ontologies.
@@ -2,10 +2,13 @@ import datetime
2
2
  import json
3
3
  import logging
4
4
  import unittest
5
- from rdflib.plugins.shared.jsonld.context import Context
5
+ from typing import Optional
6
+
6
7
  import pydantic
7
8
  import rdflib
8
- from pydantic import EmailStr, Field
9
+ from pydantic import EmailStr
10
+ from pydantic import field_validator, Field
11
+ from rdflib.plugins.shared.jsonld.context import Context
9
12
 
10
13
  from ontolutils import Thing, urirefs, namespaces, get_urirefs, get_namespaces
11
14
  from ontolutils import set_logging_level
@@ -45,6 +48,13 @@ class TestNamespaces(unittest.TestCase):
45
48
  """
46
49
  name: str = Field(default=None, alias="lastName") # name is synonymous to lastName
47
50
  age: int = None
51
+ special_field: Optional[str] = None
52
+
53
+ @field_validator('special_field', mode="before")
54
+ @classmethod
55
+ def _special_field(cls, value):
56
+ assert value == "special_string", f"Special field must be 'special_string' not {value}"
57
+ return value
48
58
 
49
59
  agent = Agent(name='John Doe', age=23)
50
60
  self.assertEqual(agent.name, 'John Doe')
@@ -59,6 +69,17 @@ class TestNamespaces(unittest.TestCase):
59
69
  self.assertEqual(agent.name, None)
60
70
  self.assertEqual(agent.age, 23)
61
71
 
72
+ # property assignment should fail:
73
+ with self.assertRaises(pydantic.ValidationError):
74
+ agent.age = "invalid"
75
+
76
+ with self.assertRaises(pydantic.ValidationError):
77
+ agent.special_field = "invalid"
78
+ self.assertEqual(agent.special_field, None)
79
+
80
+ agent.special_field = "special_string"
81
+ self.assertEqual(agent.special_field, "special_string")
82
+
62
83
  def test_resolve_iri(self):
63
84
  ret = resolve_iri('foaf:age', context=Context(source={'foaf': 'http://xmlns.com/foaf/0.1/'}))
64
85
  self.assertEqual(ret, 'http://xmlns.com/foaf/0.1/age')
@@ -72,7 +93,8 @@ class TestNamespaces(unittest.TestCase):
72
93
  ret = resolve_iri('label', context=Context(source={'age': 'http://xmlns.com/foaf/0.1/age'}))
73
94
  self.assertEqual(ret, 'http://www.w3.org/2000/01/rdf-schema#label')
74
95
 
75
- ret = resolve_iri('label', context=Context(source={'label': {'@id': 'http://www.w3.org/2000/01/rdf-schema#label'}}))
96
+ ret = resolve_iri('label',
97
+ context=Context(source={'label': {'@id': 'http://www.w3.org/2000/01/rdf-schema#label'}}))
76
98
  self.assertEqual(ret, 'http://www.w3.org/2000/01/rdf-schema#label')
77
99
 
78
100
  ret = resolve_iri('prefix:label', Context(source={}))
@@ -93,6 +115,7 @@ class TestNamespaces(unittest.TestCase):
93
115
  def test_thing_custom_prop(self):
94
116
  """It is helpful to have the properties equal to the urirefs keys,
95
117
  however, this should not be required!"""
118
+
96
119
  @namespaces(foaf='http://xmlns.com/foaf/0.1/',
97
120
  schema='http://www.schema.org/')
98
121
  @urirefs(Affiliation='prov:Affiliation',
@@ -110,7 +133,7 @@ class TestNamespaces(unittest.TestCase):
110
133
  first_name: str = Field(default=None, alias='firstName')
111
134
  lastName: str
112
135
  age: int = None
113
- affiliation: Affiliation=None
136
+ affiliation: Affiliation = None
114
137
 
115
138
  p = Person(first_name='John', lastName='Doe', age=23)
116
139
  person_json = p.model_dump_jsonld(resolve_keys=False)
@@ -147,9 +170,9 @@ class TestNamespaces(unittest.TestCase):
147
170
  p183_from_jsonld.height
148
171
 
149
172
  def test_sort_classes(self):
150
- thing1 = Thing(label='Thing 1')
151
- thing2 = Thing(label='Thing 2')
152
- self.assertFalse(thing1 < thing2)
173
+ thing1 = Thing(label='Thing 1', id='_:1')
174
+ thing2 = Thing(label='Thing 2', id='_:2')
175
+ self.assertFalse(thing1 > thing2)
153
176
  with self.assertRaises(TypeError):
154
177
  thing1 < 4
155
178
  thing1 = Thing(label='Thing 1', id='http://example.com/thing1')
@@ -158,7 +181,7 @@ class TestNamespaces(unittest.TestCase):
158
181
 
159
182
  def test__repr_html_(self):
160
183
  thing = Thing(label='Thing 1')
161
- self.assertEqual(thing._repr_html_(), 'Thing(label=Thing 1)')
184
+ self.assertEqual(thing._repr_html_(), f'Thing(id={thing.id}, label=Thing 1)')
162
185
 
163
186
  def test_thing_get_jsonld_dict(self):
164
187
  with self.assertRaises(pydantic.ValidationError):
@@ -215,7 +238,7 @@ class TestNamespaces(unittest.TestCase):
215
238
 
216
239
  jsonld_str2 = agent.model_dump_jsonld(rdflib_serialize=True) # will assign blank node! Pop it later
217
240
  jsonld_str2_dict = json.loads(jsonld_str2)
218
- self.assertNotEqual(
241
+ self.assertDictEqual(
219
242
  json.loads(jsonld_str1),
220
243
  jsonld_str2_dict
221
244
  )
@@ -265,7 +288,7 @@ class TestNamespaces(unittest.TestCase):
265
288
  label='Agent 1',
266
289
  mbox='my@email.com'
267
290
  )
268
- self.assertEqual(agent.id, None)
291
+ self.assertNotEqual(agent.id, None)
269
292
  ns = agent.namespaces
270
293
  jsonld_string = agent.model_dump_jsonld(
271
294
  context={
@@ -283,8 +306,6 @@ class TestNamespaces(unittest.TestCase):
283
306
  self.assertEqual(thing.label, 'Agent 1')
284
307
  self.assertTrue(thing.id.startswith('_:'))
285
308
  _id = thing.id
286
- self.assertEqual(thing.pop_blank_node_id(), _id)
287
- self.assertEqual(thing.id, None)
288
309
 
289
310
  def test_schema_http(self):
290
311
  @namespaces(foaf="http://xmlns.com/foaf/0.1/",
@@ -341,7 +362,6 @@ class TestNamespaces(unittest.TestCase):
341
362
  self.assertEqual(jsonld_dict['rdfs:label'], 'Person 1')
342
363
  self.assertEqual(jsonld_dict['@type'], 'foaf:Person')
343
364
 
344
-
345
365
  def test_prov(self):
346
366
  @namespaces(prov="https://www.w3.org/ns/prov#",
347
367
  foaf="http://xmlns.com/foaf/0.1/")
File without changes
File without changes
File without changes
File without changes