How can a method know if it was acquired?
Hi, I got a folderish python product with the following method: def SKU(self): """ Return our SKU for the catalog """ return self.sku The problem is that when objects contained in instances of this product are cataloged, they acquire the SKU method, so they also get the sku property. That is bad. So what I need is something like this: def SKU(self): """ Return our SKU for the catalog """ if (I was acquired): return None else: return self.sku But I can't figure out how to do that. Help? TIA, Itai Itai Tavor "Je sautille, donc je suis." itai@iinet.net.au - Kermit the Frog -- "Supposing a tree fell down, Pooh, when we were underneath it?" -- "Supposing it didn't," said Pooh after careful thought.
Itai Tavor wrote at 2003-7-7 20:00 +1000:
I got a folderish python product with the following method:
def SKU(self): """ Return our SKU for the catalog """ return self.sku
The problem is that when objects contained in instances of this product are cataloged, they acquire the SKU method, so they also get the sku property. That is bad. So what I need is something like this:
def SKU(self): """ Return our SKU for the catalog """ if (I was acquired): return None else: return self.sku
But I can't figure out how to do that. Help?
I fear this is impossible and would not be the right way either. The right way (in my view) would be to make indexes manageable and allow to set 3 boolean properties: ALLOW_ACQUISITION= 1 # to indicate that attributes may be acquired HIDE_EXCEPTIONS= 1 # silently ignore (most) exceptions during calls ALLOW_CALL= 1 # indicate that attributes are called when callable I have implemented an "IndexBase" class that determines the value of object "obj" for index "id" in the following way: def getValue(self,obj,default= None): '''the value of *obj* with respect to this index.''' marker= self # determine base value id= self.id if self.ALLOW_ACQUISITION: val= getattr(obj,id,default) elif hasattr(aq_base(obj), id): val= getattr(obj,id) else: val= default if safe_callable(val): if not self.ALLOW_CALL: return default try: val= val() except EXCEPTIONS_NOT_TO_BE_CAUGHT: raise except: if self.HIDE_EXCEPTIONS: return default raise return val Dieter
Thanks, Dieter! Argh... this should be easy to do... another thing Zope should be doing for us... On Tuesday, July 8, 2003, at 03:55 AM, Dieter Maurer wrote:
Itai Tavor wrote at 2003-7-7 20:00 +1000:
I got a folderish python product with the following method:
def SKU(self): """ Return our SKU for the catalog """ return self.sku
The problem is that when objects contained in instances of this product are cataloged, they acquire the SKU method, so they also get the sku property. That is bad. So what I need is something like this:
def SKU(self): """ Return our SKU for the catalog """ if (I was acquired): return None else: return self.sku
But I can't figure out how to do that. Help?
I fear this is impossible and would not be the right way either.
The right way (in my view) would be to make indexes manageable and allow to set 3 boolean properties:
ALLOW_ACQUISITION= 1 # to indicate that attributes may be acquired HIDE_EXCEPTIONS= 1 # silently ignore (most) exceptions during calls ALLOW_CALL= 1 # indicate that attributes are called when callable
I have implemented an "IndexBase" class that determines the value of object "obj" for index "id" in the following way:
def getValue(self,obj,default= None): '''the value of *obj* with respect to this index.''' marker= self
# determine base value id= self.id if self.ALLOW_ACQUISITION: val= getattr(obj,id,default) elif hasattr(aq_base(obj), id): val= getattr(obj,id) else: val= default
if safe_callable(val): if not self.ALLOW_CALL: return default try: val= val() except EXCEPTIONS_NOT_TO_BE_CAUGHT: raise except: if self.HIDE_EXCEPTIONS: return default raise return val
Dieter
Itai Tavor "Je sautille, donc je suis." itai@iinet.net.au - Kermit the Frog -- "Supposing a tree fell down, Pooh, when we were underneath it?" -- "Supposing it didn't," said Pooh after careful thought.
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 On Monday, July 7, 2003, at 08:00 PM, Itai Tavor wrote:
I got a folderish python product with the following method:
def SKU(self): """ Return our SKU for the catalog """ return self.sku
The problem is that when objects contained in instances of this product are cataloged, they acquire the SKU method, so they also get the sku property. That is bad. So what I need is something like this:
def SKU(self): """ Return our SKU for the catalog """ if (I was acquired): return None else: return self.sku
But I can't figure out how to do that. Help?
Try this: from Aquisition import aq_explicit [...] def SKU(self): 'return SKU if we really have one, and not just aquired one' try: return aq_explicit(self).sku except AttributeError: return None or... def SKU(self) 'return SKU if we really have one' if hasattr(aq_explicit(self),'SKU'): return self.sky else: return None You can also catalog the meta_type of the object, and use that in the query. - -- Stuart Bishop <zen@shangri-la.dropbear.id.au> http://shangri-la.dropbear.id.au/ -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.2.1 (Darwin) iD8DBQE/CmGHh8iUz1x5geARAlkpAKDyCx9h13/+/Dxzh13fwm+/H+6+zQCggC86 GkhQC5xCIjZcTZp/hue4dZo= =1UvW -----END PGP SIGNATURE-----
participants (3)
-
Dieter Maurer -
Itai Tavor -
Stuart Bishop