(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.
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.
-----Original Message----- From: zope-admin@zope.org [mailto:zope-admin@zope.org]On Behalf Of Jon Whitener Sent: Wednesday, January 08, 2003 1:35 PM To: zope@zope.org Subject: [Zope] Changing object properties from Script (Python)
(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!
Are you trying to change property types? You can't do that with changeProperies. You'll need to remove the property then add it back with the correct type. Properties are typed even though python variables aren't.
participants (3)
-
Charlie Reiman -
Jon Whitener -
Matthew T. Kromer