Skip to main content

Pieces OS Client Python SDK Assets

Use the following methods to manage your assets with the Pieces OS Client Python SDK.

Asset Management

Create a New Asset

The create_asset() method requires the content of the asset and optional metadata. The method returns the ID of the created asset.

create_asset(content, metadata)

Parameters

NameTypeNotes
contentstring[required]
metadataFragmentMetadata[optional]

Example

from pieces_os_client.wrapper import PiecesClient
from pieces_os_client import ClassificationSpecificEnum, FragmentMetadata

# Initialize the PiecesClient
pieces_client = PiecesClient()

# Set the content and metadata for the new asset
content = "print('Hello, World!')"
metadata = FragmentMetadata(ext=ClassificationSpecificEnum.PY) # optional metadata

# Create the new asset using the content and metadata
new_asset_id = pieces_client.create_asset(content, metadata)

print(f"Created asset with ID: {new_asset_id}")

# Close the client
pieces_client.close()

Get a Specific Asset

The asset() method requires the asset ID and returns the asset as a BasicAsset object.

asset(asset_id)

Parameters

NameTypeNotes
asset_idstring[required]

Example

from pieces_os_client.wrapper import PiecesClient

# Initialize the PiecesClient
pieces_client = PiecesClient()

# Get the asset ID
asset_id = "your_asset_id"

# Retrieve the asset
asset = pieces_client.asset(asset_id)

# Close the client
pieces_client.close()

Get All Assets

The assets() method returns a list of all assets with id and name properties.

assets()

Example

from pieces_os_client.wrapper import PiecesClient

# Initialize the PiecesClient
pieces_client = PiecesClient()

# Get all assets and print their names
assets = pieces_client.assets()
for asset in assets:
print(f"Asset Name: {asset.name}")

# Close the client
pieces_client.close()

BasicAsset Class

The BasicAsset class provides a way to manage assets with various properties and methods.

Properties

NameTypeNotes
idstringThe ID of the asset.
assetAssetShould only be used for properties not available in the BasicAsset class.
raw_contentstringThe content of the asset.
is_imageboolBoolean indicating if the asset is an image.
classificationClassificationSpecificEnumThe specific language classification of the asset. To get the string value, you must use asset.classification.value
namestringThe name of the asset.
descriptionstringThe description of the asset.
annotationsAnnotationsThe annotations of the asset.

Methods

create()

Creates a new asset.

Parameters
NameTypeNotes
raw_contentstringThe raw content of the asset.
metadataOptional[FragmentMetadata]The metadata of the asset.
Returns
TypeNotes
stringThe ID of the created asset.
Example
from pieces_os_client.wrapper import PiecesClient
from pieces_os_client.wrapper.basic_identifier.asset import BasicAsset
from pieces_os_client import ClassificationSpecificEnum, FragmentMetadata

# Initialize the PiecesClient
pieces_client = PiecesClient()

# Create a new asset
content = "print('Hello, World!')"
metadata = FragmentMetadata(ext=ClassificationSpecificEnum.PY)
new_asset_id = BasicAsset.create(content, metadata)
print(f"New Asset ID: {new_asset_id}")

# Close the client
pieces_client.close()

delete()

Deletes the asset.

Example
# Retrieve the asset
asset = pieces_client.asset(asset_id)

# Delete the asset
asset.delete()

# Close the client
pieces_client.close()

raw_content

Updates the raw content of the asset.

Example
# Retrieve the asset
asset = pieces_client.asset(new_asset_id)

# Get the asset content
content = asset.raw_content
print(f"Original Content: {content}")

# Update the content
asset.raw_content = content + "\n# This is a comment"
print(f"Updated Content: {asset.raw_content}")

# Close the client
pieces_client.close()

classification

Updates the classification of the asset.

Example
# Retrieve the asset
asset = pieces_client.asset(new_asset_id)

# Get the asset classification
classification = asset.classification.value if asset.classification else "None"
print(f"Asset Classification: {classification}")

# Update the classification
asset.classification = ClassificationSpecificEnum.SH # Reclassify to shell
print(f"New Classification: {classification}")

# Close the client
pieces_client.close()

name

Updates the name of the asset.

Example
# Retrieve the asset
asset = pieces_client.asset(new_asset_id)

# Get the asset name
print(f"Current Asset Name: {asset.name}")

# Update the name
asset.name = "Updated Asset Name"
print(f"Updated Asset Name: {asset.name}")

# Close the client
pieces_client.close()

Full Example

from pieces_os_client.wrapper import PiecesClient
from pieces_os_client.basic_asset import BasicAsset
from pieces_os_client import ClassificationSpecificEnum

# Initialize the PiecesClient
pieces_client = PiecesClient()

asset = pieces_client.assets()[0]

# Get the asset ID
asset_id = asset.id
print(f"Asset ID: {asset_id}")

# Get the full asset object (only for properties not available in BasicAsset)
full_asset = asset.asset
print(f"Full Asset Object: {full_asset}")

# Check if the asset is an image
if asset.is_image:
print("The asset is an image.")
else:
print("The asset is not an image.")

# Get and set the asset name
print(f"Current Asset Name: {asset.name}")
asset.name = "Updated Asset Name"
print(f"Updated Asset Name: {asset.name}")

# Retrieve and modify the asset content
content = asset.raw_content
print(f"Original Content: {content}")
asset.raw_content = content + "\n# This is a comment"
print(f"Updated Content: {asset.raw_content}")

# Get the asset classification
classification = asset.classification.value if asset.classification else "None"
print(f"Asset Classification: {classification}")

asset.classification = ClassificationSpecificEnum.SH # Reclassify to shell
print(f"New Classification: {classification}")

# Get the asset description
description = asset.description if asset.description else "No description available."
print(f"Asset Description: {description}")

# Get the asset annotations
annotations = asset.annotations if asset.annotations else "No annotations available."
print(f"Asset Annotations: {annotations}")

# Delete the asset
asset.delete()
print("Asset deleted.")

# Create a new asset
new_asset_id = BasicAsset.create("New Asset content")
print(f"New Asset ID: {new_asset_id}")

pieces_client.close()