RE: [Zope] Zope and Polymorphism
Thats really ugly having to know the distingishing method names. Isn't there some method like _.inheritsfrom(_['sequence-item'],'Folder') ???
-----Original Message----- From: Steve Spicklemire [mailto:steve@spvi.com] Sent: Tuesday, February 08, 2000 11:07 PM To: ingo.assenmacher@post.rwth-aachen.de Cc: zope@zope.org Subject: Re: [Zope] Zope and Polymorphism
Hi Ingo,
How about something like.... :
<dtml-in objectValues> <dtml-if "_.hasattr(_['sequence-item'],'isAnObjectManager')">
blah blah blah...
</dtml-if> </dtml-in>
-steve
-------------------------------------------------------------- ------------ Hi!
Since Zope is based on an OO-DB and you can add and edit objects in an intuitive manner, there is a question which does I simply can not figure out:
You can inherit methods and attributes from other objects (even multiplie inheritance), but do these objects behave like their parents in respect to identification?
To illustrate my problem:
UploadFolder inherits Folder
<dtml-in "objectValues(['Folder'])"> [...] </dtml-in>
Imagine I have several Folders together with some UploadFolders in one Folder container. The above code would simply enumerate over "real" Folders, would it not?
There are two questions:
* maybe "objectValues" is the wrong method... which one does it? * do I have to know the class name *explicitly*? (this would force me to rewrite enumerations of object every time I create new classes)
Thanks for the advices.
Regards, Ingo.
"Jay, Dylan" wrote:
Thats really ugly having to know the distingishing method names. Isn't there some method like _.inheritsfrom(_['sequence-item'],'Folder')
This doesn't look very nice to me... You could test whether an object was 'Folderish', but at the moment I have neither a clue how to do it ;-\ nor the time to sort it out ;-( <snip original comment>
UploadFolder inherits Folder
<dtml-in "objectValues(['Folder'])"> [...] </dtml-in>
Imagine I have several Folders together with some UploadFolders in one Folder container. The above code would simply enumerate over "real" Folders, would it not?
There are two questions:
* maybe "objectValues" is the wrong method... which one does it? * do I have to know the class name *explicitly*? (this would force me to rewrite enumerations of object every time I create new classes)
<And add another> but you can give objectValues more arguments (the argument _is_ a list). So that would mean in this case <dtml-in "objectValues(['Folder', 'UploadFolder',AnyOtherMetatypeYouCanThinkOf, ...])"> [bla] </dtml-in> Note that this can include any metatype you want, not just folders or 'folderish' objects. If you want you can filter them further using their attributes, properties or whatever. Rik Hoekstra
Hi Rik. Am 09-Feb-00 schrieb Rik Hoekstra:
Thats really ugly having to know the distingishing method names. Isn't there some method like _.inheritsfrom(_['sequence-item'],'Folder')
This doesn't look very nice to me...
No, it does not... but it would come in handy... :)
You could test whether an object was 'Folderish', but at the moment I have neither a clue how to do it ;-\ nor the time to sort it out ;-(
That is ok. Knowing that an object is 'Folderish' would be enough of a clue for example to provide a standarised image for a folder object within a tree.
but you can give objectValues more arguments (the argument _is_ a list). So that would mean in this case
<dtml-in "objectValues(['Folder', 'UploadFolder',AnyOtherMetatypeYouCanThinkOf, ...])"> [bla] </dtml-in>
Note that this can include any metatype you want, not just folders or 'folderish' objects. If you want you can filter them further using their attributes, properties or whatever.
That is of course right. But let me point out a little example I have in mind: Right now I am constructing a container object to which arbitrary files can be uploaded and it is then dislplayed within a table. For that purpose I created a container object which can contain very general "document objects". To those I provided some basic information which must be included into every upload (description, timestamp and who-did-the-upload stuff). I thought it would be nice to derive some objects from this baseclass (dokument) which can have more properties (keywords, references etc.). On an abstract level I basically want to do the same with these objects: display them (plus properties) in a table. This could generally be done by method overloading. Ok so far. Since it could be possible to mix several dokument-sub classes in one container it would be very nice NOT to name the items to be enumerated explicitly (like providing more arguments to the objectValues method). Right now I could use the "feature" of polymorphism very much. That was what raised my question. If there is no such thing a polymorphism... well I can live with that and do some workarounds. After all it would "only" be very comfortable to simply do method overloading and let Zope call the right method. This would enable a single declaration of <dtml-in "objectValues(['Dokument'])"> [display data here, even from derived objects] </dtml-in> instead of adjusting the parameter with every new sub-class of Dokument. Regards, Ingo ------------------------------------------
Hi about something like this (External method): # # # Check a classes base types for a certain meta_type... # # def checkClassMetaType(theClass, meta_type): """ check a class... and all super classess for meta_type....""" result = 0 if theClass.meta_type == meta_type: result = 1 else: for subClass in theClass.__bases__: result = checkClassMetaType( subClass, meta_type) if result: break return result def checkObjectMetaType(self, object, meta_type): """ check and object for an ancestral meta_type...""" if object.meta_type == meta_type: return 1 else: return checkClassMetaType(object.__class__, meta_type) ----------------------------------------------------------------------
"Ingo" == Ingo Assenmacher <ingo.assenmacher@post.rwth-aachen.de> writes:
Ingo> Hi Rik. Ingo> Am 09-Feb-00 schrieb Rik Hoekstra: >>> Thats really ugly having to know the distingishing method >>> names. Isn't there some method like >>> _.inheritsfrom(_['sequence-item'],'Folder') >>> >> This doesn't look very nice to me... Ingo> No, it does not... but it would come in handy... :) >> You could test whether an object was 'Folderish', but at the >> moment I have neither a clue how to do it ;-\ nor the time to >> sort it out ;-( >> Ingo> That is ok. Knowing that an object is 'Folderish' would be Ingo> enough of a clue for example to provide a standarised image Ingo> for a folder object within a tree. >> but you can give objectValues more arguments (the argument _is_ >> a list). So that would mean in this case >> >> <dtml-in "objectValues(['Folder', >> 'UploadFolder',AnyOtherMetatypeYouCanThinkOf, ...])"> [bla] >> </dtml-in> >> >> Note that this can include any metatype you want, not just >> folders or 'folderish' objects. If you want you can filter them >> further using their attributes, properties or whatever. Ingo> That is of course right. But let me point out a little Ingo> example I have in mind: Ingo> Right now I am constructing a container object to which Ingo> arbitrary files can be uploaded and it is then dislplayed Ingo> within a table. For that purpose I created a container Ingo> object which can contain very general "document objects". To Ingo> those I provided some basic information which must be Ingo> included into every upload (description, timestamp and Ingo> who-did-the-upload stuff). I thought it would be nice to Ingo> derive some objects from this baseclass (dokument) which can Ingo> have more properties (keywords, references etc.). On an Ingo> abstract level I basically want to do the same with these Ingo> objects: display them (plus properties) in a table. This Ingo> could generally be done by method overloading. Ok so Ingo> far. Since it could be possible to mix several dokument-sub Ingo> classes in one container it would be very nice NOT to name Ingo> the items to be enumerated explicitly (like providing more Ingo> arguments to the objectValues method). Right now I could use Ingo> the "feature" of polymorphism very much. That was what Ingo> raised my question. If there is no such thing a Ingo> polymorphism... well I can live with that and do some Ingo> workarounds. After all it would "only" be very comfortable Ingo> to simply do method overloading and let Zope call the right Ingo> method. This would enable a single declaration of Ingo> <dtml-in "objectValues(['Dokument'])"> [display data here, Ingo> even from derived objects] </dtml-in> Ingo> instead of adjusting the parameter with every new sub-class Ingo> of Dokument. Ingo> Regards, Ingo Ingo> ------------------------------------------ Ingo> _______________________________________________ Zope Ingo> maillist - Zope@zope.org Ingo> http://lists.zope.org/mailman/listinfo/zope ** No cross Ingo> posts or HTML encoding! ** (Related lists - Ingo> http://lists.zope.org/mailman/listinfo/zope-announce Ingo> http://lists.zope.org/mailman/listinfo/zope-dev )
Hi Jay, Well.. if you have a bunch of ZClasses with the same behavior, say they can all do the cha-cha, you could just give them a property (canDoTheChaCha) and check for that. Then they don't need to subclass from anything in particular, so long as they conform to any cha-cha like behavior expectations. ;-) -steve
"Jay," == Jay, Dylan <djay@lucent.com> writes:
Jay,> Thats really ugly having to know the distingishing method Jay,> names. Isn't there some method like Jay,> _.inheritsfrom(_['sequence-item'],'Folder') Jay,> ??? >> -----Original Message----- From: Steve Spicklemire >> [mailto:steve@spvi.com] Sent: Tuesday, February 08, 2000 11:07 >> PM To: ingo.assenmacher@post.rwth-aachen.de Cc: zope@zope.org >> Subject: Re: [Zope] Zope and Polymorphism >> >> >> >> Hi Ingo, >> >> How about something like.... : >> >> <dtml-in objectValues> <dtml-if >> "_.hasattr(_['sequence-item'],'isAnObjectManager')"> >> >> blah blah blah... >> >> </dtml-if> </dtml-in> >> >> -steve >>
Isn't there some method like _.inheritsfrom(_['sequence-item'],'Folder')
There's issubclass() in Python. Is this what you're looking for? issubclass (class1, class2) Return true if class1 is a subclass (direct or indirect) of class2. A class is considered a subclass of itself. If either argument is not a class object, a TypeError exception is raised. -jfarr ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Hi! I'm a signature virus. Copy me into your .sig to join the fun! ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Hi Johnothan, I think the problem with these (issubclass, isinstance) is that they deal with class objects, and they may not be that easy to handle in dtml. On the other hand, meta_types are just strings... so they can be easily created/changed. I still like the idea of simply defining some property in the base class that I can quickly check for in any instance i.e., <dtml-in objectValues> <dtml-if "_.hasattr(_['sequence-item'],'iCanDoTheChaCha')"> blah blah blah.... </dtml-if> </dtml-in> but if you're dead set against that I don't understand why you couldn't use something like: <dtml-in objectValues> <dtml-if "checkObjectMetaType(_['sequence-item'], metaTypeToCheck)"> blah blah blah.... </dtml-if> </dtml-in> where 'metaTypeToCheck' could be "Dokument" as mentioned in another post, and 'checkObjectMetaType' is defined as below. -steve ---------------------------------------------------------------------- # # # Check a classes base types for a certain meta_type... a little # safer this time..... # # def checkClassMetaType(theClass, meta_type): """ check a class... and all super classess for meta_type....""" result = 0 if hasattr(theClass, 'meta_type') and theClass.meta_type == meta_type: result = 1 else: for subClass in theClass.__bases__: result = checkClassMetaType( subClass, meta_type) if result: break return result def checkObjectMetaType(self, object, meta_type): """ check and object for an ancestral meta_type...""" try: if hasattr(object, 'meta_type') and object.meta_type == meta_type: return 1 else: return checkClassMetaType(object.__class__, meta_type) except AttributeError: return 0 ----------------------------------------------------------------------
"Jonothan" == Jonothan Farr <jfarr@real.com> writes:
>> Isn't there some method like >> _.inheritsfrom(_['sequence-item'],'Folder') Jonothan> There's issubclass() in Python. Is this what you're Jonothan> looking for? Jonothan> issubclass (class1, class2) Return true if class1 is a Jonothan> subclass (direct or indirect) of class2. A class is Jonothan> considered a subclass of itself. If either argument is Jonothan> not a class object, a TypeError exception is raised. Jonothan> -jfarr
participants (5)
-
Ingo Assenmacher -
Jay, Dylan -
Jonothan Farr -
Rik Hoekstra -
Steve Spicklemire