objects as values in a python dictionary
This concerns both Python Script and External Methods. I haven't tried it with DTML (if possible). A dictionary can use objects as values I think. res={} res[string] = "A wicked module I use often" res[context] = "This is where you were :)" That works, but it doesn't work when I loop over objectValues for object in folderobject.objectValues(): res[object] = "yeah" The error message is AttributeError and __hash__ something. What have I missed? Peter
Peter Bengtsson wrote:
This concerns both Python Script and External Methods. I haven't tried it with DTML (if possible).
A dictionary can use objects as values I think. res={} res[string] = "A wicked module I use often" res[context] = "This is where you were :)" That works, but it doesn't work when I loop over objectValues
for object in folderobject.objectValues(): res[object] = "yeah"
The error message is AttributeError and __hash__ something. What have I missed?
The keys of a python dict need to be immutable. The values can be anything. objectValues() returns a list of objects, which are complex items, not the sort of thing allowable as a dictionary key. The id of the object should work, since it is a string and thus immutable. for object in folderobject.objectValues(): res[object.getId()] = "yeah" -- Jim Washington
Thanks for the reply. I'll satisfy with that as an answer for now, even though stuff in objectValues() appear equally as complex as context or self or something like that. I need to do this for a search function. It recursivly loops through a couple of meta_type's (my ZClass objects) including Folder and collects (in theory) a dictionary of objects and their "search-match-result" The resulting dictionary will look like this (again, in theory): {<pobject at 74500>:3, <object at 05464>:1} Where the 3 and 1 represents "points" derived from my special search conditions. If I just store the Id of each object, I'm also going to need the context it has to be able to do: print getattr(itscontext, thisid).title_or_id() # in zope My alternatives are to store them like: [[<pobject at 74500>,3], [<object at 93483>,1]] I don't want to use ZopeFind because I want to have more defined control over the arrangment of the search result. Perhaps this will work: {[<pobject at 74500>]:3, [<object at 05464>]:1} ...and then I can do has_key() still. Peter
Peter Bengtsson wrote:
This concerns both Python Script and External Methods. I haven't tried it with DTML (if possible).
A dictionary can use objects as values I think. res={} res[string] = "A wicked module I use often" res[context] = "This is where you were :)" That works, but it doesn't work when I loop over objectValues
for object in folderobject.objectValues(): res[object] = "yeah"
The error message is AttributeError and __hash__ something. What have I missed?
The keys of a python dict need to be immutable. The values can be anything.
objectValues() returns a list of objects, which are complex items, not the sort of thing allowable as a dictionary key.
The id of the object should work, since it is a string and thus immutable.
for object in folderobject.objectValues(): res[object.getId()] = "yeah"
-- Jim Washington
participants (2)
-
Jim Washington -
Peter Bengtsson