Sugar.datastore.datastore: Difference between revisions
No edit summary |
|||
Line 1: | Line 1: | ||
{{Sugar Almanac TOC}} |
{{Sugar Almanac TOC}} |
||
= Datastore Helper Functions = |
|||
=== How do I create a new datastore object? === |
=== How do I create a new datastore object? === |
||
In addition to the [[Sugar.activity.activity#How_do_I_implement_a_write_file_method_for_my_activity_in_order_to_persist_my_activity_in_the_journal.3F | write_file()]] method, which allows sugar's activity code to save your activity by creating a datastore object, you can explicitly create datastore objects in your activity code using the datastore.create() |
In addition to the [[Sugar.activity.activity#How_do_I_implement_a_write_file_method_for_my_activity_in_order_to_persist_my_activity_in_the_journal.3F | write_file()]] method, which allows sugar's activity code to save your activity by creating a datastore object, you can explicitly create datastore objects in your activity code using the datastore.create() helper function. This function will return an object of type DSObject. The following code shows how a new datastore object is created and then actually written to the datastore. Other sections will discuss how this datastore object can actually be assigned to files and populated with useful metadata. |
||
<pre> |
<pre> |
||
Line 16: | Line 19: | ||
return my_dsobject |
return my_dsobject |
||
</pre> |
</pre> |
||
= Class: DSObject = |
|||
=== How do I create new metadata entries or reassign metadata for a datastore object that has been created? === |
|||
Every DSObject created by datastore.create() has a metadata property. This property refers to a DSMetadata object, which contains the metadata for your datastore object. The code below shows how to assign metadata values by referring to the metadata property of a DSObject. |
|||
<pre> |
|||
#my_dsobject is of type datastore.DSObject |
|||
my_dsobject = datastore.create() |
|||
#Map the 'filename' property to a specific filename |
|||
my_dsobject.metadata['filename'] = 'krugman-ebooks.txt' |
|||
datastore.write(my_dsobject) |
|||
</pre> |
|||
= Class: DSMetadata = |
|||
= Notes = |
= Notes = |
Revision as of 21:01, 18 June 2008
Sugar Almanac for Developers |
---|
Sugar Almanac Main Page Package: sugar |
Package: sugar.activity |
Package: sugar.graphics |
Package: sugar.datastore |
Logging |
Notes on using Python Standard Logging in Sugar |
Internationalization |
Datastore Helper Functions
How do I create a new datastore object?
In addition to the write_file() method, which allows sugar's activity code to save your activity by creating a datastore object, you can explicitly create datastore objects in your activity code using the datastore.create() helper function. This function will return an object of type DSObject. The following code shows how a new datastore object is created and then actually written to the datastore. Other sections will discuss how this datastore object can actually be assigned to files and populated with useful metadata.
#This method creates a datastore object that will be saved for later use by this activity. def _create_ds_object(self): #my_dsobject is of type datastore.DSObject my_dsobject = datastore.create() # ... you can put any code to change your datastore file and metadata here ... #Persist this newly created datastore object in the datastore for later access. datastore.write(my_dsobject) return my_dsobject
Class: DSObject
How do I create new metadata entries or reassign metadata for a datastore object that has been created?
Every DSObject created by datastore.create() has a metadata property. This property refers to a DSMetadata object, which contains the metadata for your datastore object. The code below shows how to assign metadata values by referring to the metadata property of a DSObject.
#my_dsobject is of type datastore.DSObject my_dsobject = datastore.create() #Map the 'filename' property to a specific filename my_dsobject.metadata['filename'] = 'krugman-ebooks.txt' datastore.write(my_dsobject)
Class: DSMetadata
Notes
<references />