SEQanswers

SEQanswers (http://seqanswers.com/forums/index.php)
-   General (http://seqanswers.com/forums/forumdisplay.php?f=16)
-   -   Error while python code terse (http://seqanswers.com/forums/showthread.php?t=47669)

bio_informatics 10-22-2014 08:14 AM

Error while python code terse
 
Hi Members,

I am having a hard time with python code, making it terse. One liners. I see a lot on stackoverflow using lambda. I am unable to cope up with it.

Would really appreciate any input in this regard.

So here is how I am running the file.
Quote:

python read_pdb_file.py 1iw6.pdb 1m0l.pdb
Code:

def checkExtension2(*file_name):
# trying to shrink the code..
    [print i+" doesn't end with .pdb" for i in file_name if not i.endswith(".pdb")]


def checkExtension(*file_name): # this runs fine
    for i in file_name:
        if not i.endswith(".pdb"):
            print i+" doesn't end with .pdb"
            sys.exit(1)

if __name__=="__main__":
    checkExtension2(sys.argv[1],sys.argv[2])

Error:
[print i+" doesn't end with .pdb" for i in file_name if not i.endswith(".pdb")]
^
SyntaxError: invalid syntax

Please advise.

cliffbeall 10-22-2014 10:36 AM

It doesn't know what to do with the print inside the list comprehension.

If you really want it in one line, how about:

To print the first one:
Code:

print [i+" doesn't end with .pdb" for i in file_name if not i.endswith(".pdb")][0]
To print all:
Code:

print '\n'.join([i+" doesn't end with .pdb" for i in file_name if not i.endswith(".pdb")])

bio_informatics 10-22-2014 11:18 AM

Thanks for replying.
Could you please elaborate why and what is happening?
I work a lot on file parsing. A lot of code is to check/verfify, and I want to polish my coding on them.


All times are GMT -8. The time now is 03:26 PM.

Powered by vBulletin® Version 3.8.9
Copyright ©2000 - 2019, vBulletin Solutions, Inc.