[Zope] Modifying a Dictionaries in an Extenal Method

Edward Huixquic huixquic at gmail.com
Sun Jun 5 20:21:15 EDT 2005


Dieter: Thanks again for your time and your excellent help.

Below  are some comments of my own to yours message, (hope the post
doesn't become  too mangled and hard to read).

On 6/5/05, Dieter Maurer <dieter at handshake.de> wrote:
> Edward Huixquic wrote at 2005-6-5 00:05 -0500:
> > ...
> ><dtml-if process >
> >   <dtml-in fields mapping>
> >      <dtml-call "pyUpdate(REQUEST)">
> 
> Why do you call "pyUpdate" in a loop?

Well, actually I am getting the "fields"  data from a ZSQLMethod 
dictionaries(), so, I am trying to do something to each record (in the
list itself and later on I "post" changes into the database, hence the
python loop, as shown in the pyUpdate external method i used in my
example.

As you may have guessed so far, I am a newbie, trying to figure out
this great Zope technology. Powerful but hard to  learn, as you
probably heard before, I am trying to grab that Zope spark...fighting
against namespaces, acquisition,  and other abstract  Zope concepts.


> 
> > ...
> >Name: <input type="text" name="fields.name:records" value="Mickey"><br>
> 
> Thus "fields" becomes a list of "ZPublisher.HTTPRequest.record"
> objects.

Got it, no dictionarries then its a totally different object type that
just has a dictionary looks. !

> > ...
> >Python External Method:
> >def pyUpdate(self,REQUEST):
> >    for item in range(len(self.REQUEST['fields'])):
> >        self.REQUEST['x']=self.REQUEST['fields'][0]
> >        self.REQUEST['y']=self.REQUEST['fields'][1]
> >#       self.REQUEST['fields'][0]['balance']=5000    <-----I will
> >refer to this as first line
> >#       self.REQUEST['y']['balance']=5000       <-------- this would
> >be the second line
> >    return self.REQUEST
> 
> Ouch! This code hurts my eyes!

Mine also hurt (maybe a bit less than yours,:) )  even I am a newbie,
I wrote it that way in my example as to make sure there were no weird
things happening in between wach assignment (more weird things than I
already had in hand)     . x and y are of course very bad variable 
names and the sample code I wrote really hurts, as you said.

> 
> I suggest, you avoid code duplication -- this is more efficient
> and more readable:
> 
> def pyUpdate(self,REQUEST):
>     fields = REQUEST['fields']
>     for item in range(len(fields)):
>         REQUEST['x'] = fields[0]
>         y =REQUEST['y'] = fields[1]
>         fields[0].balance = 5000
>         y.balance = 5000
> 
> This is not much more readable, isn't it?

Readable, you bet,   Efficient: don't know yet,  now I know since you
mentioned, but I just gotta "field test it", that is for sure part of
the Python learning process as well.

> 
> It is still stupid to execute the assignments in a loop (as
> they are indepentent of the loop variable) but I assume
> that you simple provides some example code (you hopefully
> do not use in your real program).

mmmm.   Actually I have done some little programs like this, I must confess.
In my example, if "fields" actually come from a
Zsqlmethod.dictionaries()  object, what would be the best way to pass
thru all and every single one of the records in the list ?

> 
> > ...
> >y      {'balance': '2000', 'lastname': 'Duck', 'name': 'Donald'}
> >x      {'balance': '1000', 'lastname': 'Mouse', 'name': 'Mickey'}
> >
> >So, fields behaves as a list and X and Y are dictionaries, right?
> 
> They look like dictionaries but are in
> fact "ZPublisher.HTTPRequest.record" instances.

Note Taken  ! 

Thanks again for your help and time, I really appreciate you taking
the time to helo all of us newbies on this list.      Best regards.

Edward


More information about the Zope mailing list