[Zope] Using string.split() on a DTML method
Doug Hellmann
hellmann@GNNcast.net
Tue, 12 Oct 1999 15:16:06 +0000
Oscar Picasso wrote:
>
> Try this, I think it should work:
> <dtml-with "_.namespace(var1=_.string.split(_['category_list_cookie'], ':'))">
>
> Often replacing name_of_my_variable by _['name_of_my_variable'] produce
> better results.
Did that work? I don't understand it either, so I'm interested in
why/how it would, if anyone has insight...
> I even don't know why in this case and I gave up trying to find the reasons
> until I can get a good documentation explaning how and why things work in
> Zope.
I would love to see some developer code for Zope. Reading through
implementations of objects is educational, but it is hard to keep an eye
on the big picture and find all of the implications of the various
tricks used while you are trying to sort out code at the same time.
> An idea: there are about 1500 members in zope.org. If everyone gives $200
> the total is $300 000. For that amount I think we could find a good team of
> writers that could quickly write real good and complete documentation not
> only targeted to top gun programmers in Zope.
How would DC feel about someone outside their company writing a book on
the subject? Would they support such a project?
> What do you think about that? If DC could also give its opinion. For my
> part I would give up to $400 just for that.
I was able to convince my company to buy support for Zope from DC. We
decided that if we had problems, we wanted the safety net and the rates
were quite reasonable. I have been very impressed with the support I
have received, even from the sales reps! I don't mean that as a slight
to sales reps in general, but the ones from DC stood out as very
knowledgeable about their product.
> At 21:04 99-10-09 -0400, Jim Cain wrote:
> >I have a DTML method, category_list_cookie, which returns a string of
> >colon-separated values I use elsewhere. I would like to split this into a
> >list so I can use something like phred.count('whatever') to see if this
> >value is present.
> >
> >However, when I do something like:
> >
> ><dtml-with "_.namespace(var1=_.string.split(category_list_cookie, ':'))">
If category_list_cookie is a DTML method, then you don't want to split
it, you want to split its return value. The <dtml-var> tag lets you
cheat by figuring out for you that category_list_output is callable,
calling it, and returning the representation of the output instead of
the method object itself. However, when you start dealing with
expressions in quotes, you are moving out of DTML and into Python.
> >I get this error:
> >
> >Error Type: TypeError
> >Error Value: argument 1: expected read-only character buffer,
> >ImplicitAcquirerWrapper found
Python is reporting its confusion as a TypeError exception, telling you
that the parameter you passed to string.split() is not a "read-only
character buffer" as required by the split function, but is instead an
"ImplicitAcquirerWrapper." I haven't actually seen that object type
before, but it sounds like something from Zope (rather than any kind of
object from the Python standard library) created to handle the fact that
category_list_cookie is not a property of the current object, but was
Acquired from the object hierarchy in your server.
To get the output of the method, the real value you want to work with,
you have to call the method by adding () after category_list_cookie:
<dtml-with "_.namespace(var1=_.string.split(category_list_cookie(),
':'))">
^^^^
You could also (re)write category_list_cookies as an ExternalMethod
which returns a Python list object. Then, since the result would be a
sequence over which you could iterate, you could use it with <dtml-in>:
<ul>
<dtml-in category_list_cookie_as_eternal_method>
<li><dtml-var sequence-item>
</dtml-in>
</ul>
Or take the length of the result directly (without splitting the
results):
<dtml-var "len(category_list_cookie_as_external_method())">
Doug