Re: [Zope-dev] Python Script
Hi Steve, In the zclass definition I have a bunch of Image objects together with the following script. SCRIPT s_center(width, height, image): import string result = None for item in container.objectItems(): if string.strip(item[0]) == image: image_width = int(item[1].getProperty("width")) image_height = int(item[1].getProperty("height")) left = (int(width) - image_width) / 2 top = (int(height) - image_height) /2 result = "left:" + str(left) + "px; top:" + str(top) + "px" return result /END_SCRIPT This script works fine if I test it inside the test tabs of the zclass. But if I run it from an instance 'container.objectItems()' returns nothing. I understand why but my problem is how to get access of the images from the instance. What I need is something giving a reference to the zclass definition from one of its instance. I think I can use the DOM model and walk through until I find it but it should be better to have a direct access from the instance itself, wrong? Thanks, Cyril
Cyril Elkaim wrote:
This script works fine if I test it inside the test tabs of the zclass. But if I run it from an instance 'container.objectItems()' returns nothing. I understand why but my problem is how to get access of the images from the instance.
What I need is something giving a reference to the zclass definition from one of its instance. I think I can use the DOM model and walk through until I find it but it should be better to have a direct access from the instance itself, wrong?
From an external method, you can do
dir(self.__class__) or dir(type(self)) to get the attributes of your class. From a PythonScript, you can use something like container.Control_Panel.Products.product_name.class_name to get a reference to the ZClass definition. You should also be able to use this: container.manage_addProduct['product_name'].class_name I suggest you make a PythonScript or external method in your ZClass definition called list_attributes, that returns a list of the attributes of the ZClass definiton. Your other methods can call that. That way, if you change the Product of your ZClass in the future, you only have one thing to alter (should you choose the PythonScript). I advise against using an external method for this, as (if you're not careful) it may return unwrapped objects, which is a security hazard. -- Steve Alexander Software Engineer Cat-Box limited http://www.cat-box.net
Cyril Elkaim wrote:
for item in container.objectItems(): if string.strip(item[0]) == image: image_width = int(item[1].getProperty("width")) image_height = int(item[1].getProperty("height"))
Also, a stylistic point: In Python, you can use something called "tuple unpacking" or "list unpacking" that can often help to make your code clearer if you find yourself referencing the elements of a tuple by index. Thus, you can rewrite your code, quoted above, as below: for name,object in container.objectItems(): if string.strip(name) == image: image_width = int(object.getProperty("width")) image_height = int(object.getProperty("height")) -- Steve Alexander Software Engineer Cat-Box limited http://www.cat-box.net
Steve Alexander wrote:
for name,object in container.objectItems(): if string.strip(name) == image: image_width = int(object.getProperty("width")) image_height = int(object.getProperty("height"))
Thanks again :-), I passed too much time with Javascript and not enough with Python, but that will changed soon :-) Cyril
participants (2)
-
Cyril Elkaim -
Steve Alexander