[Zope-dev] New Counter Product

Pavlos Christoforou pavlos@gaaros.msrc.sunysb.edu
Wed, 24 Mar 1999 19:43:25 -0500 (EST)


On Wed, 24 Mar 1999, Shaw, Howard wrote:

> Well, it is basically finished and working, combining a visible Product
> interface and a counter tag... But, overriding __setstate__, removing
> the temporary entry from the state, and called Persistent.__setstate__,
> while generating no errors, fails to prevent the temporary entry from
> being written into the database.  Please help, I need two answers!
> 
> 1. How can I create a variable within a Product that will not be
> persistent (i.e. will not be written to disk)?

ZODB cannot detect changes to mutable objects that are not treated as
immutable. So I suppose that if your object includes a list, a dictionary
or a non persistence instance then any changes to these objects will go
undetected by ZODB.

For instance you can have a counter class like:

class Counter:
    def __init__(self):
        self.__counter=1
        self.__startdate=DateTime.DateTime()
    def incr(self):
        self.__counter=self.__counter+1
    def decr(self):
        self.__counter=self.__counter-1
    def reset(self):
        self.__init__()
    def start_date(self):
        return self.__startdate
    def hits(self):
        return repr(self.__counter)
    def __repr__(self):
        return repr(self.__counter)
    __str__=__repr__

If you add an instance of the above class on a Zope object, lets say by
calling an external method:

def counter(self,name):
    setattr(self,name,Counter())

then calling

<!--# call "counter('main_counter')"-->

will (hopefully!) add a counter to your calling zope object, even if that
is a DTML method, and get registered with the ZODB.

calling 

<!--# call "main_counter.incr()"-->

will update the internal counter of your instance without registering the
new value of the internal counter with ZODB. You could for example set a
Zope property to the value of the internal counter every so many hits and
that will register with ZODB, or you could even write it in an external
file in the var directory or something.

the above example is lousy for many reasons, as I have found out, but I
hope will provide some clues to your questions.


Pavlos