Item:OSW659a81662ff44af1b2b6febeee7c3a25
(Install package: OSW Docs - Core) |
(Update package: OSW Docs - Core) |
||
| Line 1: | Line 1: | ||
==Overview== | ==Overview== | ||
{{Template:Editor/DrawIO|file_name=diagram-json|page_name=Item:OSWab674d663a5b472f838d8e1eb43e6784|uuid=95a74be1-e22d-4b6e-9e4f-836127d5915a|full_width=0}} | osw-python aims to simplify the application of linked data and linked data schemas stored in knowledge graphs in python{{Template:Editor/DrawIO|file_name=diagram-json|page_name=Item:OSWab674d663a5b472f838d8e1eb43e6784|uuid=95a74be1-e22d-4b6e-9e4f-836127d5915a|full_width=0}} | ||
In a nutshell, it only requires a few lines to | |||
* build required data classes from the knowledge graph | |||
* load instances explicit or on demand from the knowledge graph | |||
* call function through bound controller classes | |||
* store created instances in the knowledge graph | |||
<syntaxhighlight lang="python" line="1"> | |||
# illustrative example | |||
osw.load("<schema iri or query>") | |||
foo : model.Foo = osw.get("<instance iri or query>") | |||
print(foo.bar.id) | |||
baz = foo.cast(controller.Foo).calc() | |||
osw.put(baz) | |||
</syntaxhighlight>{{Template:Editor/DrawIO | |||
| file_name = OSW Python Package core concepts | |||
| page_name = Item:OSW659a81662ff44af1b2b6febeee7c3a25 | |||
| uuid = 71701a09-2d74-4393-ab8d-591efbab3856 | |||
| full_width = 0 | |||
}} | |||
==JSON-Schema and Data Classes== | ==JSON-Schema and Data Classes== | ||
| jsondata | |||
|---|---|---|---|
| Line 1: | Line 1: | ||
{ | { | ||
"required_predecessor": [ | |||
"Item:OSW7113f5cf921a4c82ad1872afeff9d01d", | |||
"Item:OSWf4a9514baed04859a4c6c374a7312f10" | |||
], | |||
"type": [ | |||
"Category:OSW494f660e6a714a1a9681c517bbb975da" | |||
], | |||
"uuid": "659a8166-2ff4-4af1-b2b6-febeee7c3a25", | |||
"name": "OswPythonPackage", | |||
"label": [ | |||
{ | |||
"text": "OSW Python Package", | |||
"lang": "en" | |||
} | |||
], | |||
"description": [ | |||
{ | |||
"text": "Demonstrates Python code generation from Wiki Categories", | |||
"lang": "en" | |||
} | |||
] | |||
} | } | ||
Revision as of 05:06, 13 January 2026
| OSW Python Package ID: OSW659a81662ff44af1b2b6febeee7c3a25 | UUID: 659a8166-2ff4-4af1-b2b6-febeee7c3a25 | 📦: world.opensemantic.meta.docs.core | |
|---|---|
| ID | OSW659a81662ff44af1b2b6febeee7c3a25 |
| UUID | 659a8166-2ff4-4af1-b2b6-febeee7c3a25 |
| Label | OSW Python Package |
| Machine compatible name | OswPythonPackage |
| Types/Categories | Tutorial |
| Statements (outgoing) | |
| Statements (incoming) | |
|
|
|
| Details | |
Description[edit source]
Demonstrates Python code generation from Wiki Categories
| Item | |
|---|---|
| Type(s)/Category(s) | Tutorial |
| Tutorial | |
|---|---|
| Prerequisites (required) | Transcend wikitext to store and edit structured & linked data JSON-SCHEMA Tutorial |
| Prerequisites (optional) | |
| Follow-up (recommended) | |
Overview[edit | edit source]
osw-python aims to simplify the application of linked data and linked data schemas stored in knowledge graphs in python
In a nutshell, it only requires a few lines to
- build required data classes from the knowledge graph
- load instances explicit or on demand from the knowledge graph
- call function through bound controller classes
- store created instances in the knowledge graph
# illustrative example
osw.load("<schema iri or query>")
foo : model.Foo = osw.get("<instance iri or query>")
print(foo.bar.id)
baz = foo.cast(controller.Foo).calc()
osw.put(baz)
JSON-Schema and Data Classes[edit | edit source]
Codegeneration from jsonschema slots for Category pages with osw-python and datamodel-code-generator
Category:MyCategory
jsonschema
{
"type": "object",
"properties": {
"text": { "type": "string" },
"number": { "type": "number" },
"array": { "type": "array" }
}
}
Category:MySubCategory
jsonschema
{
"type": "object",
"allOf": "/wiki/Category:MyCategory?action=raw&slot=jsonschema",
"properties": {
"additional_property": { "type": "string" }
}
}
Generated Python Code
class MyClass():
text: str
number: float
array: List[Any]
class MySubClass(MyClass):
additional_property: str
Data Class Instance Serialization[edit | edit source]
JSON generation from python class instances with pydantic BaseModel
Python Dataclass
class MyClass(BaseModel):
text: str
number: float
array: List[Any]
class MySubClass(MyClass):
additional_property: str
Python Class Instance
my_instance = MySubClass(
text="some text",
number=1.1,
array=[1, "two", 3.0]
additional_property = "test2"
)
my_instance.json()
my_instance = osw.store_entity(my_instance) # wiki upload
Uploaded json by calling
my_instance.json()
{
"text": "some text",
"number": 1.0,
"array": [1,"two",3.0],
"additional_property": "test2"
}
Controller Extension of Data Classes[edit | edit source]
jsondata
| required_predecessor |
| |||||
|---|---|---|---|---|---|---|
| type |
| |||||
| uuid | "659a8166-2ff4-4af1-b2b6-febeee7c3a25" | |||||
| name | "OswPythonPackage" | |||||
| label |
| |||||
| description |
|