[Zope] Changing object properties from Script (Python)

Matthew T. Kromer matt@zope.com
Wed, 08 Jan 2003 16:58:05 -0500


Jon Whitener wrote:

>(Newbie stops, wipes pieces of wall from forehead, and asks for help.)
>
>I'm stumped trying to use a Script (Python) to change the properties
>of a sequence of Zope objects.
>
>Here's the problem: certain objects (simple folderish objects of my
>'Cable Channel' ZClass) have a property called 'categories' that needs
>to be in the form of a Python *list*.  In many instances, however, the
>property was saved as a *string*.  (For more detail, see my January 6
>post "Solution for 'in requires character as left operand' error".)
>
>In my Script (Python), I've been successful identifying the problem
>objects and retrieving from them their 'categories' string.  Also, I
>can convert the string into a "singleton" list.  Then I hit problems.
>
>I want to iterate over these objects, calling a method that will
>change each one's 'categories' property.  Hell if I can do this!
>
>I create a list of two-item tuples, the first element of each is a
>reference (of some sort - the problem may lie here) to a 'Cable
>Channel' object with the problem 'categories' property.  The second
>item of each tuple is a proper Python list version of the property.
>
>I've been beating my head against things like:
>
>  object_tuple[0].manage_changeProperties({'categories':object_tuple[1]})
>
>I'm missing something about how to call a method on an object from a
>Script Python (and the object is indicated by a variable or index).
>Please help, if you can!  My newbie hackery is included below.
>
>I thank you in advance,
>Jon Whitener
>Detroit Michigan USA
>  
>

Without evaluating your whole program, I had a problem with this statement:

>        # will be a single character.
>        if len(channel.categories[0]) < 2:
>  
>

I think what you want is

                    if same_type(channel.catagories, ''):

the same_type function is a special python scripts builtin which allows 
you to compare the types of two objects, so in this case, its comparing 
channel.categories to a string.  You can't use the normal type() builtin 
in a python script, which is why the same_type() builtin is provided.

Secondly, the property manager "knows" what type your property is.  If 
your type is declared as a string property, then the type manager will 
convert your tuple back into a string.  I think you want to use type 
"lines" as the type of your categories property, so that that property 
manager will save the argument as a list of strings.  String is the 
default, by the way.