Back to index

python-biopython  1.60
test_ParserSupport.py
Go to the documentation of this file.
00001 # Copyright 1999 by Jeffrey Chang.  All rights reserved.
00002 # This code is part of the Biopython distribution and governed by its
00003 # license.  Please see the LICENSE file that should have been included
00004 # as part of this package.
00005 
00006 import string
00007 import sys
00008 from Bio import File
00009 from Bio import ParserSupport
00010 
00011 # pyUnit
00012 
00013 def pb(b):
00014     if b:
00015         return 1
00016     return 0
00017 
00018 ### TaggingConsumer
00019 
00020 print "Running tests on TaggingConsumer"
00021 
00022 class TestHandle:
00023     def write(self, s):
00024         print s
00025         
00026 h = TestHandle()
00027 tc = ParserSupport.TaggingConsumer(handle=h, colwidth=5)
00028 tc.start_section()  # '***** start_section\n'
00029 tc.test1('myline')  # 'test1: myline\n'
00030 tc.end_section()    # '***** end_section\n'
00031 
00032 
00033 
00034 
00035 ### is_blank_line
00036 
00037 print "Running tests on is_blank_line"
00038 
00039 is_blank_line = lambda *args, **keywds: \
00040                 pb(ParserSupport.is_blank_line(*args, **keywds))
00041 
00042 print is_blank_line('\n')                              # 1
00043 print is_blank_line('\r\n')                            # 1
00044 print is_blank_line('\r')                              # 1
00045 print is_blank_line('')                                # 1
00046 print is_blank_line('', allow_spaces=1)                # 1
00047 print is_blank_line('', allow_spaces=0)                # 1
00048 print is_blank_line(string.whitespace, allow_spaces=1) # 1
00049 print is_blank_line('hello')                           # 0
00050 print is_blank_line('hello', allow_spaces=1)           # 0
00051 print is_blank_line('hello', allow_spaces=0)           # 0
00052 print is_blank_line(string.whitespace, allow_spaces=0) # 0
00053 
00054 
00055 ### safe_readline
00056 
00057 print "Running tests on safe_readline"
00058 
00059 data = """This
00060 file"""
00061 
00062 h = File.UndoHandle(File.StringHandle(data))
00063 
00064 safe_readline = ParserSupport.safe_readline
00065 print safe_readline(h)    # "This"
00066 print safe_readline(h)    # "file"
00067 try: safe_readline(h)
00068 except ValueError: print "correctly failed"
00069 else: print "ERROR, should have failed"
00070 
00071 
00072 ### safe_peekline
00073 
00074 print "Running tests on safe_peekline"
00075 safe_peekline = ParserSupport.safe_peekline
00076 
00077 data = """This
00078 file"""
00079 
00080 h = File.UndoHandle(File.StringHandle(data))
00081 
00082 print safe_peekline(h) # "This"
00083 h.readline()
00084 print safe_peekline(h) # "file"
00085 h.readline()
00086 try: safe_peekline(h)
00087 except ValueError: print "correctly failed"
00088 else: print "ERROR, should have failed"
00089 h.saveline('hello')
00090 print safe_peekline(h) # 'hello'
00091 
00092 
00093 ### read_and_call
00094 
00095 print "Running tests on read_and_call"
00096 
00097 data = """>gi|132871|sp|P19947|RL30_BACSU 50S RIBOSOMAL PROTEIN L30 (BL27)
00098 MAKLEITLKRSVIGRPEDQRVTVRTLGLKKTNQTVVHEDNAAIRGMINKVSHLVSVKEQ
00099 >gi|132679|sp|P19946|RL15_BACSU 50S RIBOSOMAL PROTEIN L15
00100 MKLHELKPSEGSRKTRNRVGRGIGSGNGKTAGKGHKGQNARSGGGVRPGFEGGQMPLFQRLPKRGFTNIN
00101 RKEYAVVNLDKLNGFAEGTEVTPELLLETGVISKLNAGVKILGNGKLEKKLTVKANKFSASAKEAVEAAG
00102 GTAEVI
00103 
00104 
00105 """
00106 
00107 h = File.UndoHandle(File.StringHandle(data))
00108 
00109 rac = ParserSupport.read_and_call
00110 lines = []
00111 def m(line):
00112     lines.append(line)
00113     
00114 rac(h, m)
00115 print lines[-1][:10]   # '>gi|132871'
00116 rac(h, m, start='MAKLE', end='KEQ', contains='SVIG')
00117 rac(h, m, blank=0)
00118 
00119 # These should be errors.  If they're not, then complain.
00120 try: rac(h, m, blank=1)
00121 except ValueError: print "correctly failed"
00122 else: print "ERROR, should have failed"
00123 try: rac(h, m, start='foobar')
00124 except ValueError: print "correctly failed"
00125 else: print "ERROR, should have failed"
00126 try: rac(h, m, end='foobar')
00127 except ValueError: print "correctly failed"
00128 else: print "ERROR, should have failed"
00129 try: rac(h, m, contains='foobar')
00130 except ValueError: print "correctly failed"
00131 else: print "ERROR, should have failed"
00132 try: rac(h, m, blank=0)
00133 except ValueError: print "correctly failed"
00134 else: print "ERROR, should have failed"
00135         
00136 
00137 
00138 
00139 ### attempt_read_and_call
00140 
00141 print "Running tests on attempt_read_and_call"
00142 
00143 data = """>gi|132871|sp|P19947|RL30_BACSU 50S RIBOSOMAL PROTEIN L30 (BL27)
00144 MAKLEITLKRSVIGRPEDQRVTVRTLGLKKTNQTVVHEDNAAIRGMINKVSHLVSVKEQ
00145 >gi|132679|sp|P19946|RL15_BACSU 50S RIBOSOMAL PROTEIN L15
00146 MKLHELKPSEGSRKTRNRVGRGIGSGNGKTAGKGHKGQNARSGGGVRPGFEGGQMPLFQRLPKRGFTNIN
00147 RKEYAVVNLDKLNGFAEGTEVTPELLLETGVISKLNAGVKILGNGKLEKKLTVKANKFSASAKEAVEAAG
00148 GTAEVI"""
00149 
00150 h = File.UndoHandle(File.StringHandle(data))
00151 
00152 arac = lambda *args, **keywds: \
00153        pb(ParserSupport.attempt_read_and_call(*args, **keywds))
00154 lines = []
00155 def m(line):
00156     lines.append(line)
00157 
00158 print arac(h, m, contains="RIBOSOMAL PROTEIN")   # 1
00159 print arac(h, m, start="foobar")                 # 0
00160 print arac(h, m, blank=1)                        # 0
00161 print arac(h, m, end="LVSVKEQ")                  # 1
00162