3. Updating KItems with the SDK

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.

[13]:
from dsms import DSMS, KItem

Now source the environmental variables from an .env file and start the DSMS-session.

[14]:
dsms = DSMS(env=".env")

Now lets get the kitem we created in the 2nd tutorial : Creation of Kitems

[15]:
item = dsms.kitems[-1]
print(item.name)
Machine-1

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), we can simply use the standard list-method as we know them from basic Python (e.g. for the annotations, attachments, external_link, etc).

Other properties which are not list-like can be simply set by attribute-assignment (e.g. name, slug, ktype_id, etc).

[16]:
item.name = "Machine-1"
item.custom_properties.Producer = "Machinery GmBH"
item.attachments.append("testfile.txt")
item.annotations.append("www.machinery.org/")
item.external_links.append(
    {"url": "http://machine.org", "label": "machine-link"}
)
item.contacts.append({"name": "machinesupport", "email": "machinesupport@group.mail"})
item.affiliations.append("machine-team")
item.user_groups.append({"name": "machinegroup", "group_id": "123"})
[17]:
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:

[19]:
item
[19]:
KItem(

        name = Machine-1,

        id = dd091666-a7c9-4b3b-8832-910bdec5c63c,

        ktype_id = testing-machine,

        in_backend = True,

        slug = machine-1-dd091666,

        annotations = [
                {
                        iri: www.machinery.org/,
                        name: ,
                        namespace: www.machinery.org,
                        description: None
                }
        ],

        attachments = [
                {
                        name: testfile.txt
                }
        ],

        linked_kitems = [],

        affiliations = [
                {
                        name: machine-team
                }
        ],

        authors = [
                {
                        user_id: 7f0e5a37-353b-4bbc-b1f1-b6ad575f562d
                }
        ],

        avatar_exists = False,

        contacts = [
                {
                        name: machinesupport,
                        email: machinesupport@group.mail,
                        user_id: None
                }
        ],

        created_at = 2024-08-19 18:12:11.338394,

        updated_at = 2024-08-19 18:12:11.338394,

        external_links = [
                {
                        label: machine-link,
                        url: http://machine.org/
                }
        ],

        kitem_apps = [],

        summary = None,

        user_groups = [
                {
                        name: machinegroup,
                        group_id: 123
                }
        ],

        custom_properties = {
                Producer: Machinery GmBH,
                Location: A404,
                Model Number: Bending Test Machine No 777
        },

        dataframe = None,

        rdf_exists = False
)

Furthermore we can also download the file we uploaded again:

[18]:
for file in item.attachments:
    download = file.download()

    print("\t\t\t Downloaded file:", file)
    print("|------------------------------------Beginning of file------------------------------------|")
    print(download)
    print("|---------------------------------------End of file---------------------------------------|")
                         Downloaded file: {
                        name: testfile.txt
                }
|------------------------------------Beginning of file------------------------------------|
This is a calibration protocol!
|---------------------------------------End of file---------------------------------------|