Question

This is part of my script:

try:    
    read2length = len(reads[1])
    x2 = data[read1length:read1length+read2length,0]
    y2 = data[read1length:read1length+read2length,1]
    fig = plt.figure()
    plt.bar(x2,y2, align='center')
    fig.suptitle('Read 2 Camera Timeouts', fontsize=20)
    plt.xlabel('Cycle', fontsize=18)
    plt.ylabel('#', fontsize=16)
    if read2length < 50:
        plt.xticks(x1, fontsize=14)
    fig.savefig(join((path),'Read 2 Camera Timeouts.jpg'))
except: pass 
try:
    read3length = len(reads[2])
    x3 = data[read1length+read2length:read1length+read2length+read3length,0]
    y3 = data[read1length+read2length:read1length+read2length+read3length,1]
    fig = plt.figure()    
    plt.bar(x3,y3, align='center')
    fig.suptitle('Read 3 Camera Timeouts', fontsize=20)
    plt.xlabel('Cycle', fontsize=18)
    plt.ylabel('#', fontsize=16)
    if read3length < 50:
        plt.xticks(x1, fontsize=14)
    fig.savefig(join((path),'Read 3 Camera Timeouts.jpg'))
except: pass          

I want the script to try the first one and the second one. whether they work or not i want the script to continue.

I keep getting read2length is not defined?

Whats going on?

Was it helpful?

Solution

If the expression len(reads[1]) throws an exception (say, IndexError) then the name read2length is never assigned to.

This then leads to an exception if your next try block tries to use that name anyway.

You should really not use pokemon exception handling (you don't want to catch them all, really). Keep your exception handling as close to the exception-throwing code as possible, and only handle specific exceptions.

For example, if you want to handle reads[1] throwing an index error, then handle just that exception:

try:
    read2length = len(reads[1])
except IndexError:
    # not enough elements here; assume 0
    read2length = 0
else:
    x2 = data[read1length:read1length+read2length,0]
    y2 = data[read1length:read1length+read2length,1]
    fig = plt.figure()
    plt.bar(x2,y2, align='center')
    fig.suptitle('Read 2 Camera Timeouts', fontsize=20)
    plt.xlabel('Cycle', fontsize=18)
    plt.ylabel('#', fontsize=16)
    if read2length < 50:
        plt.xticks(x1, fontsize=14)
    fig.savefig(join((path),'Read 2 Camera Timeouts.jpg'))

Now read2length is bound to an integer, always.

OTHER TIPS

this:

try:    
    read2length = len(reads[1])
    x2 = data[read1length:read1length+read2length,0]
    y2 = data[read1length:read1length+read2length,1]
    fig = plt.figure()
    plt.bar(x2,y2, align='center')
    fig.suptitle('Read 2 Camera Timeouts', fontsize=20)
    plt.xlabel('Cycle', fontsize=18)
    plt.ylabel('#', fontsize=16)
    if read2length < 50:
        plt.xticks(x1, fontsize=14)
    fig.savefig(join((path),'Read 2 Camera Timeouts.jpg'))
except: pass 
try:
    read3length = len(reads[2])
    x3 = data[read1length+read2length:read1length+read2length+read3length,0]
    y3 = data[read1length+read2length:read1length+read2length+read3length,1]
    fig = plt.figure()    
    plt.bar(x3,y3, align='center')
    fig.suptitle('Read 3 Camera Timeouts', fontsize=20)
    plt.xlabel('Cycle', fontsize=18)
    plt.ylabel('#', fontsize=16)
    if read3length < 50:
        plt.xticks(x1, fontsize=14)
    fig.savefig(join((path),'Read 3 Camera Timeouts.jpg'))
except: pass
print 'foo'

results in

$ python q2.py
foo

on my system. My guess is you're referencing read2length after the second try-catch block.

And pay attention to what everyone else is saying about "except: pass" being bad practise.

If read2length is not defined, that the problem is on line 2, where len(reads[1]) throws an exception.

Print the reads out to see, why.

You shall also modify your except: pass and use:

except:
    print "Unexpected error:", sys.exc_info()[0]

Do not forgot to import sys before

You define read2length on your first try here:

try:    
    read2length = len(reads[1])

Which means if that try fails then read2length never gets defined. As a result when you try to use it in the second try:

try:
    read3length = len(reads[2])
    x3 = data[read1length+read2length:read1length+read2length+read3length,0]
    y3 = data[read1length+read2length:read1length+read2length+read3length,1]

it will always fail due to the udnefined variable.

Change it by making the except on the first try to:

except:
    read2length = 0

Edit: Martijn beat me too it, but his answer is better as you shouldn't have generic excepts (or use pass).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top