0

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.

8
  • 1
    How, exactly, are those two functions connected? Commented Jul 17, 2010 at 3:31
  • @Tim, I changed the indentation because it was misleading (and indentation is everything in python) Commented Jul 17, 2010 at 3:32
  • 1
    @Tim, the indentation in the code you posted was all wrong, so it's impossible to understand your problem. Please check it and edit your Q (hint: don't use tabs -- indent with four spaces per level only). Not sure @Brendan correctly read your mind in his indentation edit, since your problem might easily be connected to wrong indents (depending, as @Greg rightly asks, on how the two seemingly isolated functions actually "connect"). Commented Jul 17, 2010 at 3:32
  • I didn't even notice the indentation in the second function. I would fix it too but it might be the cause of your error.. I reverted my previous edit just in case. Commented Jul 17, 2010 at 3:33
  • do_blast returns a integer (job_ID) if the file is not empty which is then retrieved from a URL by form.getfirst and passed through to return_blast Commented Jul 17, 2010 at 3:35

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

2 Comments

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, please fix it in the question too so we know how your code actually looks.
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.