[Tutor] Checking for file completion.

Tony Pelletier tony.pelletier at gmail.com
Tue Feb 21 23:20:01 CET 2012


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

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


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

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
FREE LEARN TO PLAY HOCKEY PROGRAM.docx
TestWordDoc.docx
WeeklyDeliveriesReport_2012-02-21.csv

Done zipping attachments
.
Starting file transfer...
File attachments.zip successfully transferred.


>
>
>
>> Now, if I uncomment that time.sleep(3), it runs flawlessly which I
>> think tells me that the file is still being written to.  I *think*...
>>
>> Two questions.
>> 1.  Is that not it and if so, am I missing something obvious?
>> 2.  If it is it, is there a way to check to see if the file I'm trying
>> to read from is done being written to?
>>
>> Thanks
>>
>
>
> --
>
> DaveA


More information about the Tutor mailing list