3. Updating KItems with the SDK#
In this tutorial we see how to update existing Kitems.
3.1. Setting up#
Before you run this tutorial: make sure to have access to a DSMS-instance of your interest, alongwith with installation of this package and have establised access to the DSMS through DSMS-SDK (refer to Connecting to DSMS)
Now let us import the needed classes and functions for this tutorial.
[1]:
from dsms import DSMS, KItem
Now source the environmental variables from an .env file and start the DSMS-session.
[2]:
dsms = DSMS(env=".env")
Now lets get the kitem we created in the 2nd tutorial : Creation of Kitems
[3]:
item = dsms["ff1316ef-790c-4833-915c-de13c5f3ba91"]
[4]:
item
[4]:
kitem:
name: Specimen123
id: ff1316ef-790c-4833-915c-de13c5f3ba91
ktype_id: specimen
slug: specimen123-ff1316ef
annotations: []
attachments:
- name: subgraph.ttl
linked_kitems: []
affiliations: []
authors:
- user_id: 7f0e5a37-353b-4bbc-b1f1-b6ad575f562d
avatar_exists: false
contacts: []
created_at: 2025-04-10 10:29:24.395884
updated_at: 2025-04-10 10:29:24.395884
external_links: []
apps: []
user_groups: []
custom_properties:
content:
sections:
- id: id0595fb5f1853a8
name: Untitled Section
entries:
- id: idba92210e193128
type: Number
label: Width
value: 0.5
measurementUnit:
iri: http://qudt.org/vocab/unit/MilliM
label: Millimetre
symbol: null
namespace: http://qudt.org/vocab/unit
relationMapping: null
required: false
- id: ida08bcc9977e388
type: Slider
label: Length
value:
- 0.1
- 0.2
measurementUnit:
iri: http://qudt.org/vocab/unit/MilliM
label: Millimetre
symbol: null
namespace: http://qudt.org/vocab/unit
relationMapping: null
required: false
rdf_exists: true
3.2. Updating Kitems#
Now, we would like to update the properties of our KItem we created previously.
Depending on the schema of each property (see DSMS KItem Schema), listaccumulation (+= or -=), e.g. for the annotations, attachments, external_link, etc.
NOTE: using append or extend will not validate the pydantic model of the respective fields and hence will cause an error during commiting. Hence, always use +=, = or -=.
Other properties which are not list-like can be simply set by attribute-assignment (e.g. name, slug, ktype_id, etc).
[5]:
item.name = "Specimen-123"
item.custom_properties.Width = 1
item.attachments += ["testfile.txt"]
item.annotations += ["https://w3id.org/pmd/co/Specimen"]
item.external_links += [
{"url": "http://specimens.org", "label": "specimen-link"}
]
item.contacts += [{"name": "Specimen preparation", "email": "specimenpreparation@group.mail"}]
[6]:
dsms.add(item)
dsms.commit()
We can see now that the local system path of the attachment is changed to a simply file name, which means that the upload was successful. If not so, an error would have been thrown during the commit.
We can see the updates when we print the item:
[7]:
item
[7]:
kitem:
name: Specimen-123
id: ff1316ef-790c-4833-915c-de13c5f3ba91
ktype_id: specimen
slug: specimen123-ff1316ef
annotations:
- iri: https://w3id.org/pmd/co/Specimen
label: Specimen
namespace: https://w3id.org/pmd/co
attachments:
- name: subgraph.ttl
- name: testfile.txt
linked_kitems: []
affiliations: []
authors:
- user_id: 7f0e5a37-353b-4bbc-b1f1-b6ad575f562d
avatar_exists: false
contacts:
- name: Specimen preparation
email: specimenpreparation@group.mail
created_at: 2025-04-10 10:29:24.395884
updated_at: 2025-04-10 10:30:00.537758
external_links:
- label: specimen-link
url: http://specimens.org
apps: []
user_groups: []
custom_properties:
content:
sections:
- id: id0595fb5f1853a8
name: Untitled Section
entries:
- id: idba92210e193128
type: Number
label: Width
value: 1
measurementUnit:
iri: http://qudt.org/vocab/unit/MilliM
label: Millimetre
symbol: null
namespace: http://qudt.org/vocab/unit
relationMapping: null
required: false
- id: ida08bcc9977e388
type: Slider
label: Length
value:
- 0.1
- 0.2
measurementUnit:
iri: http://qudt.org/vocab/unit/MilliM
label: Millimetre
symbol: null
namespace: http://qudt.org/vocab/unit
relationMapping: null
required: false
rdf_exists: true
Furthermore we can also download the file we uploaded again:
[8]:
for file in item.attachments:
download = file.download()
print("\t\t\t Downloaded file:", file.name)
print("|------------------------------------Beginning of file------------------------------------|")
print(download)
print("|---------------------------------------End of file---------------------------------------|\n\n")
Downloaded file: subgraph.ttl
|------------------------------------Beginning of file------------------------------------|
@prefix ns1: <http://qudt.org/schema/qudt/> .
@prefix ns2: <https://w3id.org/steel/ProcessOntology/> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
<https://bue.materials-data.space/knowledge/specimen/specimen123-ff1316ef> a ns2:TestPiece ;
rdfs:label "Specimen-123"^^xsd:string ;
ns2:hasTotalLength_Object <https://bue.materials-data.space/knowledge/specimen/max_specimen123-ff1316ef>,
<https://bue.materials-data.space/knowledge/specimen/min_specimen123-ff1316ef> ;
ns2:hasWidth_Object <https://bue.materials-data.space/knowledge/specimen/emmo#EMMO_e4de48b1_dabb_4490_ac2b_040f926c64f0_specimen123-ff1316ef> .
<https://bue.materials-data.space/knowledge/specimen/emmo#EMMO_e4de48b1_dabb_4490_ac2b_040f926c64f0_specimen123-ff1316ef> a <https://w3id.org/emmo#EMMO_e4de48b1_dabb_4490_ac2b_040f926c64f0> ;
ns1:hasUnit "http://qudt.org/vocab/unit/MilliM"^^xsd:anyURI ;
ns1:value 1 .
<https://bue.materials-data.space/knowledge/specimen/max_specimen123-ff1316ef> a <https://w3id.org/emmo#EMMO_cd2cd0de_e0cc_4ef1_b27e_2e88db027bac> ;
ns1:hasUnit "http://qudt.org/vocab/unit/MilliM"^^xsd:anyURI ;
ns1:value "0.2"^^xsd:float .
<https://bue.materials-data.space/knowledge/specimen/min_specimen123-ff1316ef> a <https://w3id.org/emmo#EMMO_cd2cd0de_e0cc_4ef1_b27e_2e88db027bac> ;
ns1:hasUnit "http://qudt.org/vocab/unit/MilliM"^^xsd:anyURI ;
ns1:value "0.1"^^xsd:float .
|---------------------------------------End of file---------------------------------------|
Downloaded file: testfile.txt
|------------------------------------Beginning of file------------------------------------|
This is a specimen preparation protocol!
|---------------------------------------End of file---------------------------------------|