[Tutor] Checking for file completion.

Dave Angel d at davea.name
Tue Feb 21 23:57:12 CET 2012

On 02/21/2012 05:20 PM, Tony Pelletier wrote:
>>> def getReport(service, reportId):
>>>         reportIds = service.client.factory.create('ArrayOfstring')
>>>         reportIds.string.append(reportId)
>>>         try:
>>>                 result = service.client.service.ReportQueryById(reportIds,
>>> 'True')
>>>                 if result.Report[0].ReportStatus == 'Processing':
>>>                         print 'Report in Processing state...'
>>>                         time.sleep(3)
>>>                         getReport(service,reportId)
>> Dd you intend to call yourself recursively here?  I don't understand all
>> your logic here, but if you really did, I'd at least expect you to save the
>> return value from the recursive call.  This is also the first place you're
>> missing a return statement.
>> Is it possible you're trying to use recursion to substitute for a while
>> loop?  Generally a bad idea.
> I did intend it.  I'm trying to grab the report right after I send the
> request in, which probably isn't a great idea, but I'll worry about
> that after I figure this out.  The idea is that I know I'll get
> result.Report[0].ReportStatus to be 'Processing', so I'm sleeping and
> doing it over.  On the second go around, I get the return.

But recursion is not "go around".  It creates a new stack frame, all new 
local variables,  It calls that factory thingie again,  I have no idea 
what this library of yours does, so I can't directly advise you, but 
this code is just wrong.

>>>                 else:
>>>                         filename =
>>>   result.Report[0].ReportFileArgs.ReportFileArg[0].UserFileName
>>>                         print filename
>>>                         encodedfile =
>>> result.Report[0].ReportFileArgs.ReportFileArg[0].EncodedValue
>>>                         encodedstring = encodedfile.encode('utf-8')
>>>                         str_list = []
>>>                         for line in encodedstring:
>>>                                 line = line.rstrip()
>>>                                 str_list.append(line)
>>>                         string = ''.join(str_list)
>>>                         data = base64.b64decode(string)
>>>                         outfile = open(filename, 'w')
>>>                         outfile.write(data)
>>>                         outfile.close()
>>>                         shutil.copyfile(filename, os.path.join('files',
>>> filename))
>>>                         print filename + ' report succesfully written
>>> out...'
>>>                         logging.info(filename + ' report succesfully
>>> written out...')
>>>                         return filename
>>>         except suds.WebFault as e:
>>>                 print e.fault.detail
>> Missing a return statement in the case of an exception.
> Can you explain this?  Why would I want the return on the exception?

I'll answer your question with a question.  Just what do YOU think will 
happen after it prints the e.fault.detail stuff?  It'll return to the 
caller, with a return value of None.  You need an explicit return 
statement to give it a different, explicit value.

>>> then from main, I'm using filename to then make the call to
>>> getEventAttachments and passing in filename.  The None you see is a
>>> result of my "print filename" statement.
>> Clearly this is Python 2.x code.  print is a statement and has no return
>> value (result).  You don't state that this is the whole code, but if so,
>> then you're missing return statements from several exit points in the
>> function.  It's probably that missing return statement that gives you the
>> None.
> Not sure I agree here.  Again, if put a 3 sec pause in as mentioned
> below, I get these successful results.

You have a return statement on the else clause, so if it happens to 
execute that path, you'll get a return value for filename.

> C:\Python27\python.exe
> C:/cygwin/home/Tony/code/soaptraining/getEventAttachments.py
> Deleting "files" directory...
> "files" directory successfully created...
> WeeklyDeliveriesReport_2012-02-21.csv
> WeeklyDeliveriesReport_2012-02-21.csv report succesfully written out...
> WeeklyDeliveriesReport_2012-02-21.csv  -  This is the print statement
> that returns None
> ....
> Rows of data here I print out......
> ....
> TestWordDoc.docx successfully written out!
> FREE LEARN TO PLAY HOCKEY PROGRAM.docx successfully written out!
> bookofruby.pdf successfully written out!
> EmAlex1.jpg successfully written out!
> Creating archive...
> bookofruby.pdf
> EmAlex1.jpg
> TestWordDoc.docx
> WeeklyDeliveriesReport_2012-02-21.csv
> Done zipping attachments
> .
> Starting file transfer...
> File attachments.zip successfully transferred.

Look, you don't even include the import statements, nor mention what 
library you're using for this SOAP stuff.  So you aren't likely to get 
help from whoever happens to know that library.

Your output reflects running a lot more code than you show here, so 
we're all just guessing.

I can only give you general help on the python code, and that function 
has 3 return points, but only one of them has a return statement. It 
recurses without using the results of the inner calls.  Then just 
returns without using any of that Service code, and without returning a 

So what if you get different results with the sleep() call.  That 
changes the interaction with the unknown library, and the unknown host 
at the other end of the network. So maybe it works through that path, 
and not through this one.

You could try writing a simple function with the same structure of 
try/if/recurse/else/except  and see if you can make any sense of it.

Under the present conditions, I'm done.  Good luck.


More information about the Tutor mailing list