The PEAK Developers' Center   Diff for "IntroToPeak/LessonThree" UserPreferences
 
HelpContents Search Diffs Info Edit Subscribe XML Print View Up
Ignore changes in the amount of whitespace

Differences between version dated 2004-03-01 21:56:29 and 2005-02-01 11:15:34 (spanning 8 versions)

Deletions are marked like this.
Additions are marked like this.

    to -- displays a greeting
"""
 
    Messages = bindings.Make(
    Messages = binding.Make(
        'helloworld.storage.MessageDM', offerAs=[storage.DMFor(Message)]
    )
 

section to `hello`: {{{
 
[peak.running.shortcuts]
to = importString('helloworld.toCmd')
to = importString('helloworld.commands.toCmd')
 
}}}
 

to = importString('helloworld.commands.toCmd')
for = importString('helloworld.commands.forCmd')
 
}}} and another `AbstractCommand` subclass in `helloworld.py`
}}} and another `AbstractCommand` subclass in `commands.py`
 
{{{
#!python

== Storing a New Message: Modifying the Data Manager ==
 
OK, it's time to do some serious surgery on our Data Manager.
First, we need to exchange our `QueryDM` base class to a base
First, we need to exchange our `QueryDM` base class for a base
class that supports updating the database. That would be
`storage.EntityDM`.
 
`EntityDM` requires two additional methods to be defined by the
concrete class: `_new`, and `_save`. `_new` is called when a new
object is added to the DM, and needs to store the data for that
object is added to the DM and needs to store the data for that
object in the external database. `_save` is called when an object's
state is changed, and a transaction boundry has been reached where
that state needs to be synchronized with the external database.

        self.data[ob.forname] = {'forname':ob.forname, 'text':ob.text}
}}}
 
That was easy. The `_new()` method is responsible for both saving state and returning the object ID of the new object. This is because `_new` is responsible for assigning object IDs. In this case, we simply return `ob.forname`, since that's what we're using as an object ID), after calling `self._save(ob)`. Let's run the script, and try it out: {{{
That was easy. The `_new()` method is responsible for both saving state and returning the object ID of the new object. This is because `_new` is responsible for assigning object IDs. In this case, we simply return `ob.forname`, since that's what we're using as an object ID, after calling `self._save(ob)`. Let's run the script, and try it out: {{{
 
% ./hello for Fred: Hi, guy!
% ./hello to Fred

 
Oops. All we did was update our in-memory `data` dictionary. We didn't save it to disk, so the change didn't stay in place for long. How can we fix that?
 
If we look at the `storage.IWritableDM` interface (see `peak help storage.IWritableDM`), we'll see that it includes a `flush()` method. `flush()` is called as part of the transaction commit process, and the default implementation of this method in `EntityDM` is what calls our `_save()` and `_new()` methods for the appropriate objects. If we define our own version of `flush()`, that first calls the standard `flush()`, and then writes our `data` array to disk, we'll be all set: {{{
If we look at the `storage.IWritableDM` interface (see `peak help storage.IWritableDM`), we'll see that it includes a `flush()` method. `flush()` is called as part of the transaction commit process, and the default implementation of this method in `EntityDM` is what calls our `_save()` and `_new()` methods for the appropriate objects. If we define our own version of `flush()` that first calls the standard `flush()` and then writes our `data` array to disk, we'll be all set: {{{
#!python
    def flush(self,ob=None):
        super(MessageDM,self).flush(ob)

 
An easy way to do this would be to override the `abortTransaction()` method, similar to what we did for `flush()`, and delete the `data` dictionary if the transaction is aborted: {{{
#!python
    def abortTransaction(self, txnSvc):
        del self.data
    def abortTransaction(self, ob):
        self._delBinding("data")
        super(MessageDM,self).abortTransaction(ob)
}}}
 

        return oid in self.data
}}}
 
Now we can update our `forCmd._run()` method in `helloworld.py`: {{{
Now we can update our `forCmd._run()` method in `commands.py`: {{{
#!python
    def _run(self):
 

 
        return default
}}}
DM's have a `cache` attribute that holds on to currently loaded objects,
so that multiple requests for a given object ID, will always return the
DM's have a `cache` attribute that holds onto currently loaded objects,
so that multiple requests for a given object ID will always return the
same object. So, by checking it here first, we can avoid doing the
lookup in `self.data` if the requested object is already loaded.
 

PythonPowered
ShowText of this page
EditText of this page
FindPage by browsing, title search , text search or an index
Or try one of these actions: AttachFile, DeletePage, LikePages, LocalSiteMap, SpellCheck