Hi, I've started this mail because I had a problem. While typing I found my solution, so basically I don't have any question. I'm just learning to use Zope with Python and I'm progressing wel. But maybe someone want to look at this piece of code and can give some suggestions about it. Is it well-coded python? Can I get the same results in smarter/better ways? # file eme_changeScheduled.py # external Method eme_changeScheduled # This method is called from a DTML-method that displays a list of several # selected objects that has the property 'scheduled' and let the user # change all property values at once. # This method is invoked from the root. # REQUEST should have 'foldername' as the name of the subfolder that # contains the objects # REQUEST should have 'new_scheduled' that is a list of object names and new # scheduled values # # I'm only able to supply a string for new_scheduled that looks like: # ('1.1.1','IssueNr001','1.1.1','IssueNr002','1.2.1','IssueNr003') (etc) import string 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 def replaceScheduled(object,n_scheduled): """ replace the value of object's property scheduled with n_scheduled """ if object.hasProperty('scheduled'): object._updateProperty('scheduled',n_scheduled) object.reindex_object() 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=[] Append=Result.append Alist=list(new_scheduled) l=len(Alist) ind=0 while ind<l: object=findObject(self, Alist[ind+1]) if object: scheduled=object.getProperty('scheduled') Append(str(Alist[ind+1])+'='+str(scheduled)+str('->')+str(Alist[ind])) replaceScheduled(object, Alist[ind]) ind=ind+2 return string.join(Result,'\n') 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)
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.
This should probably be made getattr(self.aq_base, foldername, None) to avoid being bitten by unintended implicit acquisition. Stefan --On Freitag, 16. Mai 2003 15:04 +0200 Lennart Regebro <lennart@regebro.nu> wrote:
Wim Bekker wrote:
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)
-- The time has come to start talking about whether the emperor is as well dressed as we are supposed to think he is. /Pete McBreen/
participants (3)
-
Lennart Regebro -
Stefan H. Holek -
Wim Bekker