OSW Python Package
Item:OSW659a81662ff44af1b2b6febeee7c3a25
OSW Python Package [OSW659a81662ff44af1b2b6febeee7c3a25] | |
---|---|
ID | OSW659a81662ff44af1b2b6febeee7c3a25 |
UUID | 659a8166-2ff4-4af1-b2b6-febeee7c3a25 |
Label | OSW Python Package |
Machine compatible name | OswPythonPackage |
Statements (outgoing) | |
Statements (incoming) | |
|
|
Keywords |
Description
Demonstrates Python code generation from Wiki Categories
Item | |
---|---|
Type(s)/Category(s) | Tutorial |
CreativeWork |
---|
Article |
---|
Tutorial | |
---|---|
Prerequisites (required) | Transcend wikitext to store and edit structured & linked data JSON-SCHEMA Tutorial |
Prerequisites (optional) | |
Follow-up (recommended) |
Overview
[Edit]
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 |
| |||||
---|---|---|---|---|---|---|
type |
| |||||
uuid | "659a8166-2ff4-4af1-b2b6-febeee7c3a25" | |||||
name | "OswPythonPackage" | |||||
label |
| |||||
description |
|