[Tutor] loops

spir denis.spir at free.fr
Wed Apr 29 10:16:05 CEST 2009


Le Wed, 29 Apr 2009 09:42:44 +0200,
"A.T.Hofkamp" <a.t.hofkamp at tue.nl> s'exprima ainsi:

> Norman Khine wrote:
> > hello,
> > i have the following code:
> > 
> > if unique_id is not None:
> >     training = self.get_site_root()
> >     from training import Training
> >     # link back to news item
> >     root = context.root
> >     formats = ['itinerary', 'news']
> >     for document in formats:
> >         results = root.search(format=document, unique_id=unique_id)
> >         if results:
> 
> Instead of processing results from each search directly, why not first
> collect results from all formats, followed by processing of the collected
> results?
> 
> ie something like
> 
> results = []
> for doc in formats:
>     results.extend(root.search(....))
> 
> for result in results:
>     ....
> 
> This does not give you better performance. However, it does give you the
> room to re-organize the searching.
> 
> 
> In the above, you can now replace the first loop by something like
> 
> results = root.search_all_formats(formats, unique_id)
> 
> 
> Does this help?
> 
> Albert

I find the logic far too complicated, as shown by the outline of the code. 
You can be sure that in most cases (but not all?) the major complications do not lie in the problem, but in the programmer's views on it. So I would try and find other approaches to simplify the algorithm.
Also, you can abstract some of the problematic aspects of the process into funcs, that can each cope with a "limited and well-delimited" part of the problem's field. Hope I'm clear and this helps...

def foo(...):
   .......
def bar(...):
   .......
def bazzz(...):
   .......
def blurp(...):
   .......

if blurp():
   foo(...)
   while bazzz(...):
      bar(...)

Denis
------
la vita e estrany


More information about the Tutor mailing list