Item:OSW659a81662ff44af1b2b6febeee7c3a25: Difference between revisions

Item:OSW659a81662ff44af1b2b6febeee7c3a25
(Install package: OSW Docs - Core)
 
m (Protected "OSW Python Package": Protected as read-only import via Page Exchange extension ([Edit=Allow only administrators] (indefinite) [Move=Allow only administrators] (indefinite)) [cascading])
 
(One intermediate revision by the same user not shown)
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": [
"required_predecessor": [
        "Item:OSW7113f5cf921a4c82ad1872afeff9d01d",
"Item:OSW7113f5cf921a4c82ad1872afeff9d01d",
        "Item:OSWf4a9514baed04859a4c6c374a7312f10"
"Item:OSWf4a9514baed04859a4c6c374a7312f10"
    ],
],
    "type": [
"type": [
        "Category:OSW494f660e6a714a1a9681c517bbb975da"
"Category:OSW494f660e6a714a1a9681c517bbb975da"
    ],
],
    "uuid": "659a8166-2ff4-4af1-b2b6-febeee7c3a25",
"uuid": "659a8166-2ff4-4af1-b2b6-febeee7c3a25",
    "name": "OswPythonPackage",
"name": "OswPythonPackage",
    "label": [
"label": [
        {
{
            "text": "OSW Python Package",
"text": "OSW Python Package",
            "lang": "en"
"lang": "en"
        }
}
    ],
],
    "description": [
"description": [
        {
{
            "text": "Demonstrates Python code generation from Wiki Categories",
"text": "Demonstrates Python code generation from Wiki Categories",
            "lang": "en"
"lang": "en"
        }
}
    ]
]
}
}

Latest 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

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)

View as slide show

Overview

osw-python aims to simplify the application of linked data and linked data schemas stored in knowledge graphs in python

drawio: diagram-json


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)
drawio: OSW Python Package core concepts

JSON-Schema and Data Classes

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

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

Add functionalities to entities represented by wiki pages

Python Dataclass
class MyClass():
    text: str
    number: float
    array: List[Any]
    
class MySubClass(MyClass):
    additional_property: str
Python Controller Class
class MyClassController(MyClass):
    def print_text(text):
        print(self.text)
Controller instantiation and execution
my_instance = MySubClass(
    text="some text",
    # ...
)
# or
my_instance = osw.load_entity("Item:...")

my_instance = my_instance.cast(MyInstanceController)
my_instance.print_text() # "some text"
jsondata
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"