spacup
spacup

Reputation: 21

Use a variable before its declaration

I am writing a Python script and I have a problem of variable declaration, here is a part of my script :

if chromosome :
    if chromosome.group(1) != '1' :
        output.close()
        print str(base)+" bases found in chromosome "+chr_name
        chr_name = chromosome.group(1)
    output = open(current_dir+"/"+name+"3/"+name+".chr"+chromosome.group(1)+".wig", "w")
    output.write("fixedStep chrom=chr"+chromosome.group(1)+" start=1 step=1\n")

As you can see, I close my output before to create it in the code, but in the facts, this would never happens because you cannot enter the condition before to create the file.

I can make something dirty, opening a tmp file before my condition, but that's not very elegant. So I was wondering if there is another solution to make Python happy ?

EDIT :

if chromosome :
    if chromosome.group(1) != '1' :
        print str(base)+" bases found in chromosome "+chr_name
        chr_name = chromosome.group(1)
    with open(current_dir+"/"+name+"3/"+name+".chr"+chromosome.group(1)+".wig") as output:
        output.write("fixedStep chrom=chr"+chromosome.group(1)+" start=1 step=1\n")

EDIT2:

Here is an idea of the algorithm, maybe it could help understand the problem better :

for line in input do :
     chromosome = re.search(something)
     if chromosome :
          if chromosome != '1' :
              ouput.close()
          output = open(file+chromosome)
          output.write(title)
     elif somethingElse :
          output.write(somethingElse)
     endif
endfor

I want to create different files (file1, file2, file3...). Before to create file2, I have to close file1; before to create file3, I have to close file2; etc... but for the first one (file1), I don't have to close anything !

Upvotes: 0

Views: 158

Answers (4)

Mike
Mike

Reputation: 894

I'm not entirely sure what you're asking for, but I think you could easily solve this with a another nested if statement.

EDIT: too early this morning, forgot this is Python need to have something in the object

output = None
    for line in input do :
         chromosome = re.search(something)
         if chromosome :
              if chromosome != '1' :
                  if output != None : 
                       output.close()
              output = open(file+chromosome)
              output.write(title)
         elif somethingElse :
              output.write(somethingElse)

Upvotes: 0

Eric
Eric

Reputation: 97571

This looks like a candidate for itertools.groupby:

pattern = re.compile(...)
for chromasome, lines in itertools.groupby(input, key=pattern.search):
    with open(filebase + chromosome, 'w') as f:
        for line in lines:
            ...

Upvotes: 0

rlms
rlms

Reputation: 11060

Are you trying to do something like this:

if chromosome :
    if chromosome.group(1) != '1' :
        print str(base)+" bases found in chromosome "+chr_name
        chr_name = chromosome.group(1)
    else:
        output = open(current_dir+"/"+name+"3/"+name+".chr"+chromosome.group(1)+".wig", 'w')
        output.write("fixedStep chrom=chr"+chromosome.group(1)+" start=1 step=1\n")
        output.close()

        output = open("The next file")
        output.write("whatever")
        output.close()

or with improved file handling and str.format instead of adding strings:

if chromosome :
    if chromosome.group(1) != '1' :
        print str(base)+" bases found in chromosome "+chr_name
        chr_name = chromosome.group(1)
    else:
        with open({}/{}3/{}.chr{}.wig.format(current_dir,name,name,chromosome.group(1), 'w') as output:
            output.write("fixedStep chrom=chr"+chromosome.group(1)+" start=1 step=1\n")
        with open("The next file") as output:
            output.write("whatever")

Upvotes: 1

thefourtheye
thefourtheye

Reputation: 239453

You can use with statement, like this

with open(current_dir+"/"+name+"3/"+name+".chr"+chromosome.group(1)+".wig", 'w') as output:
    output.write("fixedStep chrom=chr"+chromosome.group(1)+" start=1 step=1\n")

You don't have to worry about closing the file explicitly.

Upvotes: 5

Related Questions