Item:OSW659a81662ff44af1b2b6febeee7c3a25: Difference between revisions

From OpenSemanticWorld
Item:OSW659a81662ff44af1b2b6febeee7c3a25
(Update 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])
ย 
(No difference)

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[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)

View as slide show

Overview[edit | edit source]

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[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]


๐Ÿ“Ž Select files (or drop them here)... ๐Ÿ“ท Camera
    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"
    Cookies help us deliver our services. By using our services, you agree to our use of cookies.