[Zope] No question

Lennart Regebro lennart@regebro.nu
Fri, 16 May 2003 15:04:06 +0200


Wim Bekker wrote:
> import string

Well, strings are since Python 2.0 objects with methods, so that you now 
don't have to import string and do strin.join(list, '\n'), instead you 
just do '\n'.join(list).

> def findObject(self,foldername):
>     """
>     find an object with the same name as foldername in object self
>     """
>     result=None
>     for object in self.objectValues():
>         tit=object.getId()
>         if tit==foldername:
>             result=object
>     return result

This is quicker and should work:

def findObject(self,foldername):
     """
     find an object with the same name as foldername in object self
     """
     return getattr(self, foldername, None)


> def listScheduled(self,new_scheduled):
>     """
>     Searches all objects in self that are listed in new_scheduled
>     as even entries. If found the property scheduled for the object
>     gets a new value which is the entry just before the object's title.
>     Also a list with results is generated
>     """
>     Result=[]

Ugh. Strangely Capitalized Variable Names. :)
(Ok, it's a matter of taste/religion)

 >     Append=Result.append

This, I don't like. Further down you call 'Append' and it took me 10 
seconds to figure out what the heck was going on. If you want to do 
Result.append, do Result.append. Don't rename it, it's only obfuscating.

 >     Alist=list(new_scheduled)

>     l=len(Alist)
>     ind=0
>     while ind<l:

Easier to do:
     for ind in range(0, len(Alist), 2):

So the full one would be:
def listScheduled(self,new_scheduled):
     """
     Searches all objects in self that are listed in new_scheduled
     as even entries. If found the property scheduled for the object
     gets a new value which is the entry just before the object's title.
     Also a list with results is generated
     """
     result=[]
     alist=list(new_scheduled)
     for i in range(0, len(alist), 2):
         object=self.findObject(alist[i+1])
         if object:
             scheduled=object.getProperty('scheduled')
             newstr = str(alist[i+1])+'='+str(scheduled)+\
                  str('->')+str(alist[i])
             result.append(newstr)
             self.replaceScheduled(object, alist[i])

     return '\n'.join(result)

But this is indeed a very strange way of doing it alltogether. Having a 
list of where names and values are intermixed is sure to cause problems 
sooner or later. The Python way to do this is to let new_scheduled be a 
dictionary. The method would the look like this:

def listScheduled(self,new_scheduled):
     result = []

     for key, values in new_scheduled.items():
         object = self.findObject(key)
         if object is not None:
            object.replaceScheduled(value)

+ the other stuff you did, wheich I don't feel like typing in again. ;)


> def eme_changeScheduled(self,REQUEST=None):
>     """
>     change the content of property scheduled for all objects
>     that are found in the subfolder
>     """
>     fname=REQUEST.get('foldername','')
>     nscheduled=REQUEST.get('new_scheduled','')
>     folder=findObject(self,fname)
>     listScheduled(folder,nscheduled)

Why not just:

def eme_changeScheduled(self,foldername, new_scheduled):
     """
     change the content of property scheduled for all objects
     that are found in the subfolder
     """
     folder=findObject(self,foldername)
     return listScheduled(folder,new_scheduled)

That will work even when called through the web, and the parameters will 
be picked up from request automatically.