[Zope] Modifying a Dictionaries in an Extenal Method

Tino Wildenhain tino at wildenhain.de
Mon Jun 6 00:52:59 EDT 2005


Am Sonntag, den 05.06.2005, 19:21 -0500 schrieb Edward Huixquic:
> 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).
> 
...
> 
> > > ...
> > >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?

For me, it looks a bit like:

def pyUpdate(self,REQUEST):
     fields=REQUEST['fields']
     x=REQUEST['x']=fields[0]
     y=REQUEST['y']=fields[1]
     x.balance=5000
     y.balance=5000


I dont think the loop above makes any sense :)

> 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?
> > 

The common idiom goes like this:

results = [{'balance':balanceexpression(item),
            'lastname':item.lastname,
            'name':item.name}
            for item in context.SomeZSQLMethod()]

for example.

For more advise I think we need the big picture (in english
words) what you are doing here.




More information about the Zope mailing list