[Zope] Changing object properties from Script (Python)

Jon Whitener wmmail@twmi.rr.com
Wed, 08 Jan 2003 16:35:14 -0500


(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

## Script (Python) "fix_channels_with_problem"
##bind container=container
##bind context=context
##bind namespace=
##bind script=script
##bind subpath=traverse_subpath
##parameters=
##title=
##
#

def channels_with_problem():
    # Return a list of tuples (lot).  The first element of each tuple
    # is a Cable Channel object with a problem 'categories' property.
    # The second item of each tuple is the proper list version of the
    # Cable Channel's categories property.

    problem_cc_lot = []

    for channel in context.objectValues('Cable Channel'):

        # Parse first 'list item' from the 'categories' property.
        # If categories is stored as a string, the first item 
        # will be a single character.
        if len(channel.categories[0]) < 2:

            # Convert the 'categories' value into a list, then
            # add the appropriate (object, categories) tuple to
            # the list.
            cat_list = []
            cat_list.append(channel.categories)

            channel_to_fix = (channel, cat_list)
            problem_cc_lot.append(channel_to_fix)

    return problem_cc_lot

items_to_fix = channels_with_problem()

# Everything is peachy so far, but when I try to process the items in
# items_to_fix, I get nowhere.  I want logic like this:

# for every tuple in items_to_fix:
#    take the first item (the Zope object)
#    and change its 'categories' property to the tuple's second item.