Re: [ZODB-Dev] Writing Persistent Class

2008-01-23 Thread Marius Gedminas
On Mon, Jan 21, 2008 at 07:15:42PM +0100, Dieter Maurer wrote:
 Marius Gedminas wrote at 2008-1-21 00:08 +0200:
 Personally, I'd be afraid to use deepcopy on a persistent object.
 
 A deepcopy is likely to be no copy at all.
 
   As Python's deepcopy does not know about object ids, it is likely
   that the copy result uses the same oids as the original.
   When you store this copy, objects with the same oid are identified.

This appears not to be the case.  The following script prints Looks OK:


#!/usr/bin/python
import copy
import transaction
from persistent import Persistent
from ZODB.DB import DB
from ZODB.MappingStorage import MappingStorage

class SampleObject(Persistent):
pass

db = DB(MappingStorage())
conn = db.open()
conn.root()['obj1'] = SampleObject()
transaction.commit()

# Use a different connection to sidestep the ZODB object cache
conn2 = db.open()
conn2.root()['obj2'] = copy.deepcopy(conn2.root()['obj1'])
transaction.commit()

conn3 = db.open()
obj1 = conn3.root()['obj1']
obj2 = conn3.root()['obj2']
transaction.commit()

if obj1 is obj2:
print Fail: instead of a copy we got the same object
elif obj1._p_oid == obj2._p_oid:
print Fail: copy has the same oid
else:
print Looks OK.


Marius Gedminas
-- 
I used to think I was indecisive, but now I'm not so sure.


signature.asc
Description: Digital signature
___
For more information about ZODB, see the ZODB Wiki:
http://www.zope.org/Wikis/ZODB/

ZODB-Dev mailing list  -  ZODB-Dev@zope.org
http://mail.zope.org/mailman/listinfo/zodb-dev


Re: [ZODB-Dev] Writing Persistent Class

2008-01-21 Thread Dieter Maurer
Marius Gedminas wrote at 2008-1-21 00:08 +0200:
Personally, I'd be afraid to use deepcopy on a persistent object.

A deepcopy is likely to be no copy at all.

  As Python's deepcopy does not know about object ids, it is likely
  that the copy result uses the same oids as the original.
  When you store this copy, objects with the same oid are identified.

If you are lucky, then the ZODB recognizes the problem (because,
it is unable to store two different objects (the result of the deepcopy)
with the same oid in its cache.



-- 
Dieter
___
For more information about ZODB, see the ZODB Wiki:
http://www.zope.org/Wikis/ZODB/

ZODB-Dev mailing list  -  ZODB-Dev@zope.org
http://mail.zope.org/mailman/listinfo/zodb-dev


Re: [ZODB-Dev] Writing Persistent Class

2008-01-21 Thread Kenneth Miller

Dieter,

This is fine for me I believe. I only needed to have a copy of an  
object for a short while after the ZODB connection has closed with no  
intention of ever inserting it back into zodb. Out of curiousity, what  
would be the proper way to accomplish this task?


Thanks for your time, everyone.

Regards,
Kenneth Miller

On Jan 21, 2008, at 12:15 PM, Dieter Maurer wrote:


Marius Gedminas wrote at 2008-1-21 00:08 +0200:

Personally, I'd be afraid to use deepcopy on a persistent object.


A deepcopy is likely to be no copy at all.

 As Python's deepcopy does not know about object ids, it is likely
 that the copy result uses the same oids as the original.
 When you store this copy, objects with the same oid are identified.

If you are lucky, then the ZODB recognizes the problem (because,
it is unable to store two different objects (the result of the  
deepcopy)

with the same oid in its cache.



--
Dieter
___
For more information about ZODB, see the ZODB Wiki:
http://www.zope.org/Wikis/ZODB/

ZODB-Dev mailing list  -  ZODB-Dev@zope.org
http://mail.zope.org/mailman/listinfo/zodb-dev


___
For more information about ZODB, see the ZODB Wiki:
http://www.zope.org/Wikis/ZODB/

ZODB-Dev mailing list  -  ZODB-Dev@zope.org
http://mail.zope.org/mailman/listinfo/zodb-dev


Re: [ZODB-Dev] Writing Persistent Class

2008-01-18 Thread Stephan Richter
On Thursday 17 January 2008, Kenneth Miller wrote:
       I'm trying to make one of my classes persistent. The class  
 itself has only simple attributes of general types like  
 int,string,timestamp, no lists etc. It seems to work just fine without  
 subclassing Persistent, but causes an error when I do. Do I always  
 need to subclass persistent?

Yes.

Regards,
Stephan
-- 
Stephan Richter
Web Software Design, Development and Training
Google me. Zope Stephan Richter
___
For more information about ZODB, see the ZODB Wiki:
http://www.zope.org/Wikis/ZODB/

ZODB-Dev mailing list  -  ZODB-Dev@zope.org
http://mail.zope.org/mailman/listinfo/zodb-dev


Re: [ZODB-Dev] Writing Persistent Class

2008-01-18 Thread Jim Fulton
I don't have time to offer specific suggestions or help, other than to  
say that while persistence classes are powerful, these are wildly  
advanced.  I steer clear of them myself.


Jim


On Jan 17, 2008, at 8:08 PM, Kenneth Miller wrote:


All,

Quick question.

I'm trying to make one of my classes persistent. The class  
itself has only simple attributes of general types like  
int,string,timestamp, no lists etc. It seems to work just fine  
without subclassing Persistent, but causes an error when I do. Do I  
always need to subclass persistent?


There error i receive seems to have a problem with my __eq__ method.

Here's the error I get when I do subclass persistent:

...No handlers could be found for logger ZODB.Connection
E
==
ERROR: testPersistence (__main__.ToolDataTests)
--
Traceback (most recent call last):
 File Objects.py, line 1412, in testPersistence
   self.failUnlessEqual(readFromFS(),td)
 File /System/Library/Frameworks/Python.framework/Versions/2.5/lib/ 
python2.5/unittest.py, line 332, in failUnlessEqual

   if not first == second:
 File Objects.py, line 1278, in __eq__
Below is where it's choking on my __eq__ method!!!
   return self.name == other.name and self.value == other.value and  
mx 
.DateTime.cmp(self.timeStamp,other.timeStamp,Globals.AvgTimePrec)==0  
and self.slowData == other.slowData
 File /Library/Python/2.5/site-packages/ZODB3-3.7.2-py2.5- 
macosx-10.5-i386.egg/ZODB/Connection.py, line 758, in setstate

   raise ConnectionStateError(msg)
ConnectionStateError: Shouldn't load state for 0x01 when the  
connection is closed


--
Ran 8 tests in 0.043s

FAILED (errors=1)

Thanks for the help!

Regards,
Kenneth Miller

___
For more information about ZODB, see the ZODB Wiki:
http://www.zope.org/Wikis/ZODB/

ZODB-Dev mailing list  -  ZODB-Dev@zope.org
http://mail.zope.org/mailman/listinfo/zodb-dev


--
Jim Fulton
Zope Corporation


___
For more information about ZODB, see the ZODB Wiki:
http://www.zope.org/Wikis/ZODB/

ZODB-Dev mailing list  -  ZODB-Dev@zope.org
http://mail.zope.org/mailman/listinfo/zodb-dev


Re: [ZODB-Dev] Writing Persistent Class

2008-01-18 Thread Marius Gedminas
On Thu, Jan 17, 2008 at 07:08:19PM -0600, Kenneth Miller wrote:
 There error i receive seems to have a problem with my __eq__ method.

 Here's the error I get when I do subclass persistent:

 ...No handlers could be found for logger ZODB.Connection
 E
 ==
 ERROR: testPersistence (__main__.ToolDataTests)
 --
 Traceback (most recent call last):
   File Objects.py, line 1412, in testPersistence
 self.failUnlessEqual(readFromFS(),td)
   File 
 /System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/unittest.py,
  
 line 332, in failUnlessEqual
 if not first == second:
   File Objects.py, line 1278, in __eq__
   Below is where it's choking on my __eq__ method!!!
 return self.name == other.name and self.value == other.value and 
 mx.DateTime.cmp(self.timeStamp,other.timeStamp,Globals.AvgTimePrec)==0 and 
 self.slowData == other.slowData
   File 
 /Library/Python/2.5/site-packages/ZODB3-3.7.2-py2.5-macosx-10.5-i386.egg/ZODB/Connection.py,
  
 line 758, in setstate
 raise ConnectionStateError(msg)
 ConnectionStateError: Shouldn't load state for 0x01 when the connection is 
 closed

It seems that you're keeping a reference to a persistent object after
you close the ZODB connection.  Don't do that.

Marius Gedminas
-- 
If Linux doesn't have the solution, you have the wrong problem.


signature.asc
Description: Digital signature
___
For more information about ZODB, see the ZODB Wiki:
http://www.zope.org/Wikis/ZODB/

ZODB-Dev mailing list  -  ZODB-Dev@zope.org
http://mail.zope.org/mailman/listinfo/zodb-dev


Re: [ZODB-Dev] Writing Persistent Class

2008-01-18 Thread Dieter Maurer
Kenneth Miller wrote at 2008-1-17 19:08 -0600:
 ...
Do I always  
need to subclass persistent?

When you assign an instance of your (non persistent derived) class
as an attribute to a persistent object,
then your instance will be persisted together with its persistent
container.
However, local modifications to your instance are not recognized
by the persistency mechanism. You need to explicitly inform the persistent
container about the change.

Moreover, persistent objects define the granularity with which
application and storage interact: load and store work on
the level of persistent objects excluding persistent subobjects.



-- 
Dieter
___
For more information about ZODB, see the ZODB Wiki:
http://www.zope.org/Wikis/ZODB/

ZODB-Dev mailing list  -  ZODB-Dev@zope.org
http://mail.zope.org/mailman/listinfo/zodb-dev


Re: [ZODB-Dev] Writing Persistent Class

2008-01-18 Thread Kenneth Miller

All,

 Yes, i realised that a little while ago. Thanks a ton for all of  
the responses. My solution was to use deepcopy to make a copy of the  
object to use for something else later on, is this appropriate?


Regards,
Kenneth Miller

On Jan 18, 2008, at 3:43 PM, Marius Gedminas wrote:


On Thu, Jan 17, 2008 at 07:08:19PM -0600, Kenneth Miller wrote:

There error i receive seems to have a problem with my __eq__ method.

Here's the error I get when I do subclass persistent:

...No handlers could be found for logger ZODB.Connection
E
= 
=

ERROR: testPersistence (__main__.ToolDataTests)
--
Traceback (most recent call last):
 File Objects.py, line 1412, in testPersistence
   self.failUnlessEqual(readFromFS(),td)
 File
/System/Library/Frameworks/Python.framework/Versions/2.5/lib/ 
python2.5/unittest.py,

line 332, in failUnlessEqual
   if not first == second:
 File Objects.py, line 1278, in __eq__
Below is where it's choking on my __eq__ method!!!
   return self.name == other.name and self.value == other.value and
mx 
.DateTime 
.cmp(self.timeStamp,other.timeStamp,Globals.AvgTimePrec)==0 and

self.slowData == other.slowData
 File
/Library/Python/2.5/site-packages/ZODB3-3.7.2-py2.5-macosx-10.5- 
i386.egg/ZODB/Connection.py,

line 758, in setstate
   raise ConnectionStateError(msg)
ConnectionStateError: Shouldn't load state for 0x01 when the  
connection is

closed


It seems that you're keeping a reference to a persistent object after
you close the ZODB connection.  Don't do that.

Marius Gedminas
--
If Linux doesn't have the solution, you have the wrong problem.
___
For more information about ZODB, see the ZODB Wiki:
http://www.zope.org/Wikis/ZODB/

ZODB-Dev mailing list  -  ZODB-Dev@zope.org
http://mail.zope.org/mailman/listinfo/zodb-dev


___
For more information about ZODB, see the ZODB Wiki:
http://www.zope.org/Wikis/ZODB/

ZODB-Dev mailing list  -  ZODB-Dev@zope.org
http://mail.zope.org/mailman/listinfo/zodb-dev