is an object in a folder
I'd like to add a method like def hasObject(self, id): """Test if an object is in the current object.""" for o in self._objects: if o['id'] == id: return 1 return 0 to ObjectManager. This would bring it in line with BTreeFolder2 (who already has an hasObject method) and we could then always use the most efficient method to test if a folder has a given subobject id. Opinions ? Florent -- Florent Guillaume, Nuxeo (Paris, France) +33 1 40 33 71 59 http://nuxeo.com mailto:fg@nuxeo.com
On Tue, Aug 24, 2004 at 03:25:17PM +0200, Florent Guillaume wrote:
I'd like to add a method like
def hasObject(self, id): """Test if an object is in the current object.""" for o in self._objects: if o['id'] == id: return 1 return 0
to ObjectManager.
This would bring it in line with BTreeFolder2 (who already has an hasObject method) and we could then always use the most efficient method to test if a folder has a given subobject id.
Opinions ?
I like having such a method, but let's not iterate over all the sub-objects. Why not this? from Acquisition import aq_base ... def hasObject(self, id): """Test if an object is in the current object. """ if hasattr(aq_base(self), id): return 1 return 0 The same idiom is used in ObjectManager.checkValidId, FWIW. -- Paul Winkler http://www.slinkp.com
from Acquisition import aq_base ... def hasObject(self, id): """Test if an object is in the current object. """ if hasattr(aq_base(self), id): return 1 return 0
It would be nice and faster but it doesn't work for non-subobjects attributes. For instance your hasObject('meta_type') or hasObject('isPrincipiaFolderish') would return true. The goal of hasObject, as I see it, is to check if the object is considered a real subobject. That's what BTreeFolder2's does.
The same idiom is used in ObjectManager.checkValidId, FWIW.
Yes but the use case is slightly different IMHO. Florent -- Florent Guillaume, Nuxeo (Paris, France) +33 1 40 33 71 59 http://nuxeo.com mailto:fg@nuxeo.com
Am Di, den 24.08.2004 schrieb Florent Guillaume um 17:48:
from Acquisition import aq_base ... def hasObject(self, id): """Test if an object is in the current object. """ if hasattr(aq_base(self), id): return 1 return 0
It would be nice and faster but it doesn't work for non-subobjects attributes. For instance your hasObject('meta_type') or hasObject('isPrincipiaFolderish') would return true.
Isn't that what objectIds() is for? return id in self.objectIds() Christian -- Christian Theune <ct@gocept.com> gocept gmbh & co. kg
On Thu, Aug 26, 2004 at 08:29:19AM +0200, Christian Theune wrote:
Am Di, den 24.08.2004 schrieb Florent Guillaume um 17:48:
from Acquisition import aq_base ... def hasObject(self, id): """Test if an object is in the current object. """ if hasattr(aq_base(self), id): return 1 return 0
It would be nice and faster but it doesn't work for non-subobjects attributes. For instance your hasObject('meta_type') or hasObject('isPrincipiaFolderish') would return true.
Isn't that what objectIds() is for?
return id in self.objectIds()
Object might contain lots of subobjects. Returned list might be very big. -- Maciej Pietrzak jid:horpah@jabber.org email:M.Pietrzak@prioris.mini.pw.edu.pl
Why not: def hasObject(self, id): """Test if an object is in the current object.""" return id in self.objectIds() Suresh "Florent Guillaume" <fg@nuxeo.com> wrote in message news:412B41BD.7080709@nuxeo.com...
I'd like to add a method like
def hasObject(self, id): """Test if an object is in the current object.""" for o in self._objects: if o['id'] == id: return 1 return 0
to ObjectManager.
This would bring it in line with BTreeFolder2 (who already has an hasObject method) and we could then always use the most efficient method to test if a folder has a given subobject id.
Opinions ?
Florent
-- Florent Guillaume, Nuxeo (Paris, France) +33 1 40 33 71 59 http://nuxeo.com mailto:fg@nuxeo.com _______________________________________________ Zope-Dev maillist - Zope-Dev@zope.org http://mail.zope.org/mailman/listinfo/zope-dev ** No cross posts or HTML encoding! ** (Related lists - http://mail.zope.org/mailman/listinfo/zope-announce http://mail.zope.org/mailman/listinfo/zope )
def hasObject(self, id): """Test if an object is in the current object.""" return id in self.objectIds()
Because then it always computes all ids, before testing presence (second list scan). Time is N+pos(id) whereas with the scan below, time is pos(id) (N being the total number of subobjects). Florent
def hasObject(self, id): """Test if an object is in the current object.""" for o in self._objects: if o['id'] == id: return 1 return 0
to ObjectManager.
This would bring it in line with BTreeFolder2 (who already has an hasObject method) and we could then always use the most efficient method to test if a folder has a given subobject id.
Opinions ?
Florent
-- Florent Guillaume, Nuxeo (Paris, France) +33 1 40 33 71 59 http://nuxeo.com mailto:fg@nuxeo.com
Florent Guillaume wrote at 2004-8-24 15:25 +0200:
I'd like to add a method like
def hasObject(self, id): """Test if an object is in the current object.""" for o in self._objects: if o['id'] == id: return 1 return 0
to ObjectManager.
This would bring it in line with BTreeFolder2 (who already has an hasObject method) and we could then always use the most efficient method to test if a folder has a given subobject id.
Opinions ?
I would use "hasattr(aq_base(self), id)" (as someone else already suggested). Almost always it is not relevant whether "id" identifies a true subobject (in the "ObjectManager" sense) or another attribute. -- Dieter
participants (6)
-
Christian Theune -
Dieter Maurer -
Florent Guillaume -
Maciej Pietrzak -
Paul Winkler -
sureshvv