tags:

views:

81

answers:

2

I have problem with a if statement code below:

do_blast(x):
    test_empty = open('/home/rv/ncbi-blast-2.2.23+/db/job_ID/%s.blast' % (z), 'r')
        if test_empty.read() == '':
            test_empty.close()
            return 'FAIL_NO_RESULTS'
        else:
            do_something

def return_blast(job_ID):
     if job_ID == 'FAIL_NO_RESULTS':
        return '<p>Sorry no results :( boooo</p>'
    else:
        return open('/home/rv/ncbi-blast-2.2.23+/db/job_ID/job_ID_%s.fasta' % (job_ID), 'r').read()

For some reason the code tries to assign "job_ID" to the fasta file in return_blast even though it should have returned "sorry no results". I also understand the file names and extensions are different i have my reasons for doing this.

The code works perfectly when the test_empty file is not empty.

+1  A: 

I'm not sure if this is the problem, but your code isn't indented correctly (and that matters in Python). I believe this is what you were wanting:

do_blast(x):
    test_empty = open('/home/rv/ncbi-blast-2.2.23+/db/job_ID/%s.blast' % (z), 'r')
    if test_empty.read() == '':
        test_empty.close()
        return 'FAIL_NO_RESULTS'
    else:
        do_something

def return_blast(job_ID):
    if job_ID == 'FAIL_NO_RESULTS':
        return '<p>Sorry no results :( boooo</p>'
    else:
        return open('/home/rv/ncbi-blast-2.2.23+/db/job_ID/job_ID_%s.fasta' % (job_ID), 'r').read()

I don't think your code would have even run though..

Brendan Long
The code is indented correctly if my file. It runs when the file is not empty but fails to catch when the file is.
Tim
@Tim, please fix it in the question too so we know how your code actually looks.
Brendan Long
A: 

Maybe some simple printf style debugging will help:

def return_blast(job_ID):
    print 'job_ID: ', job_ID
    # ... 

Then you can at least see what "job_ID" your function receives. This is crucial for trying to figure out why your if statement fails.

ars