Dynamic dictionary keys?
Allow me to preface this by saying I am a complete newbie to Zope (I prefer to think of it as beginner's mind...). What I am attempting to do is dynamically add items to a dictionary, but Zope complains "keyword can't be an expression". Here's the reduced case of my code: <dtml-let eventsDict="{}"> <dtml-let calstart="somestring"> <dtml-call "REQUEST.set(eventsDict[calstart]=id)"> </dtml-let> </dtml-let> This kind of construct *seems* perfectly reasonable, yet it doesn't work. Any suggestions? Other possible ways of doing the same thing? Thanks! -Andrew ----- Andrew Hedges, Clearwired Web Services andrew@clearwired.com / http://clearwired.com/
----- Original Message ----- From: "Andrew Hedges" <andrew@clearwired.com> To: <zope@zope.org> Sent: Monday, February 27, 2006 11:56 AM Subject: [Zope] Dynamic dictionary keys?
Allow me to preface this by saying I am a complete newbie to Zope (I prefer to think of it as beginner's mind...). What I am attempting to do is dynamically add items to a dictionary, but Zope complains "keyword can't be an expression". Here's the reduced case of my code:
<dtml-let eventsDict="{}"> <dtml-let calstart="somestring"> <dtml-call "REQUEST.set(eventsDict[calstart]=id)"> </dtml-let> </dtml-let>
It is much easier in python script, but if you really want to use dtml: <dtml-call "REQUEST.set('eventsDict', {})"> <dtml-call "eventsDict.update({'calstart' : id})"> hth Jonathan
On 27.02.06 09:56:49, Andrew Hedges wrote:
Allow me to preface this by saying I am a complete newbie to Zope (I prefer to think of it as beginner's mind...). What I am attempting to do is dynamically add items to a dictionary, but Zope complains "keyword can't be an expression". Here's the reduced case of my code:
<dtml-let eventsDict="{}"> <dtml-let calstart="somestring"> <dtml-call "REQUEST.set(eventsDict[calstart]=id)"> </dtml-let> </dtml-let>
First let me tell you: Write a python script for things like this, it really belongs into one. Second thing: your dtml-call has a serious error, eventsDict[..]=id is executed but it doesn't produce anything so set is called with no parameters and this is wrong. set needs 2 parameters, one beeing a string as the key and a second as the value which can be of any type python knows. I also don't get what this snippet should do? Andreas -- Today is the first day of the rest of your life.
Andreas and Jonathan, Thanks for the responses! As I said, I am completely new to Zope/ Python, so I appreciate your help. I'll look into how this could be done with a Python script instead of DTML, but this version of our product is all DTML, so that's why I'm using this solution for now. Next version we're moving to Zope 3, so things should get better! In any case, Jonathan's solution worked (though he had single quotes around 'calstart' which shouldn't have been there.) What I'm doing is setting up a hash table so I can do some branching in a separate loop. I have a feeling I'm building a hash table for something I may be able to access directly, but it's working, so I don't want to take up more space in peoples' inboxes. Thanks again for the quick help! -Andrew On Feb 27, 2006, at 10:15 AM, Andreas Pakulat wrote:
On 27.02.06 09:56:49, Andrew Hedges wrote:
Allow me to preface this by saying I am a complete newbie to Zope (I prefer to think of it as beginner's mind...). What I am attempting to do is dynamically add items to a dictionary, but Zope complains "keyword can't be an expression". Here's the reduced case of my code:
<dtml-let eventsDict="{}"> <dtml-let calstart="somestring"> <dtml-call "REQUEST.set(eventsDict[calstart]=id)"> </dtml-let> </dtml-let>
First let me tell you: Write a python script for things like this, it really belongs into one.
Second thing: your dtml-call has a serious error, eventsDict[..]=id is executed but it doesn't produce anything so set is called with no parameters and this is wrong. set needs 2 parameters, one beeing a string as the key and a second as the value which can be of any type python knows.
I also don't get what this snippet should do?
Andreas
On 2/27/06, Andrew Hedges <andrew@clearwired.com> wrote:
Allow me to preface this by saying I am a complete newbie to Zope (I prefer to think of it as beginner's mind...).
Welcome! And let's start with killing off two ideas that most newbies have (because the documentation gives it to the,): 1. Through-the-web development really isn't such a hot idea. :-) Granted, it has it's uses, but most of the time it's a pain in the ass, because: a. By necessity, the extra security needed means you have restrictions that are hard to circumvent. b. It's really hard to have verisoning and making diffs on things that reside in a ZODB. c. A text field in a browser isn't the most programmer friendly editor you can imagine. It is therefore quite likely you want to start making disk-bsed products quite quick. See http://zopewiki.org/DiskBasedProduct Since TTW development was one of the original ideas with Zope, the documentation is very focused on it, but most people notice that it's actually quicker and easier to go the disk-based way. YMMV and so forth. 2. Doing things in the template is not a good idea. As mentioned above, you want to do any kind of logic in python. That means either in a disk-based product, or in a Python-script. The Zope documentation doesn't say this clearly enough. In fact, it often encourages you to do complicated things in DTML. Besides, ZPT is a new and better templating language, you might want to use that instead of DTML. It's largely a matter of taste, I definitely prefer ZPT, but as long as you keep *all* logic from the template it becomes less important. See also: http://blogs.nuxeo.com/sections/blogs/lennart_regebro/2005_04_19_python_stat... -- Lennart Regebro, Nuxeo http://www.nuxeo.com/ CPS Content Management http://www.cps-project.org/
Hi Andrew
Allow me to preface this by saying I am a complete newbie to Zope
In that case, please skip DTML and go straight to ZPT. DTML is like the ghost train in an amusement park, but the ghosts are real. Especially if you're a newbie.
<dtml-let eventsDict="{}"> <dtml-let calstart="somestring"> <dtml-call "REQUEST.set(eventsDict[calstart]=id)"> </dtml-let> </dtml-let>
Don't code python in a template. Call a Python Script which prepares your dictionary instead. E.g. <ul tal:define="eventsDict here/getEvents">...</ul> -- jean
participants (5)
-
Andreas Pakulat -
Andrew Hedges -
Jean Jordaan -
Jonathan -
Lennart Regebro