Back to index

python-biopython  1.60
Classes | Functions | Variables
Bio.ParserSupport Namespace Reference

Classes

class  AbstractParser
class  AbstractConsumer
class  TaggingConsumer
class  SGMLStrippingConsumer
class  EventGenerator

Functions

def read_and_call
def read_and_call_while
def read_and_call_until
def attempt_read_and_call
def _fails_conditions
def is_blank_line
def safe_readline
def safe_peekline

Variables

int xml_support = 1

Function Documentation

def Bio.ParserSupport._fails_conditions (   line,
  start = None,
  end = None,
  contains = None,
  blank = None,
  has_re = None 
) [private]

Definition at line 374 of file ParserSupport.py.

00374 
00375                       has_re=None):
00376     if start is not None:
00377         if line[:len(start)] != start:
00378             return "Line does not start with '%s':\n%s" % (start, line)
00379     if end is not None:
00380         if line.rstrip()[-len(end):] != end:
00381             return "Line does not end with '%s':\n%s" % (end, line)
00382     if contains is not None:
00383         if line.find(contains) == -1:
00384             return "Line does not contain '%s':\n%s" % (contains, line)
00385     if blank is not None:
00386         if blank:
00387             if not is_blank_line(line):
00388                 return "Expected blank line, but got:\n%s" % line
00389         else:
00390             if is_blank_line(line):
00391                 return "Expected non-blank line, but got a blank one"
00392     if has_re is not None:
00393         if has_re.search(line) is None:
00394             return "Line does not match regex '%s':\n%s" % (
00395                 has_re.pattern, line)
00396     return None

Here is the call graph for this function:

Here is the caller graph for this function:

def Bio.ParserSupport.attempt_read_and_call (   uhandle,
  method,
  keywds 
)
attempt_read_and_call(uhandle, method, **keywds) -> boolean

Similar to read_and_call, but returns a boolean specifying
whether the line has passed the checks.  Does not raise
exceptions.

See docs for read_and_call for a description of the function
arguments.

Definition at line 354 of file ParserSupport.py.

00354 
00355 def attempt_read_and_call(uhandle, method, **keywds):
00356     """attempt_read_and_call(uhandle, method, **keywds) -> boolean
00357 
00358     Similar to read_and_call, but returns a boolean specifying
00359     whether the line has passed the checks.  Does not raise
00360     exceptions.
00361 
00362     See docs for read_and_call for a description of the function
00363     arguments.
00364 
00365     """
00366     line = safe_readline(uhandle)
00367     passed = not _fails_conditions(*(line,), **keywds)
00368     if passed:
00369         method(line)
00370     else:
00371         uhandle.saveline(line)
00372     return passed

Here is the call graph for this function:

Here is the caller graph for this function:

def Bio.ParserSupport.is_blank_line (   line,
  allow_spaces = 0 
)
is_blank_line(line, allow_spaces=0) -> boolean

Return whether a line is blank.  allow_spaces specifies whether to
allow whitespaces in a blank line.  A true value signifies that a
line containing whitespaces as well as end-of-line characters
should be considered blank.

Definition at line 397 of file ParserSupport.py.

00397 
00398 def is_blank_line(line, allow_spaces=0):
00399     """is_blank_line(line, allow_spaces=0) -> boolean
00400 
00401     Return whether a line is blank.  allow_spaces specifies whether to
00402     allow whitespaces in a blank line.  A true value signifies that a
00403     line containing whitespaces as well as end-of-line characters
00404     should be considered blank.
00405 
00406     """
00407     if not line:
00408         return 1
00409     if allow_spaces:
00410         return line.rstrip() == ''
00411     return line[0] == '\n' or line[0] == '\r'

Here is the caller graph for this function:

def Bio.ParserSupport.read_and_call (   uhandle,
  method,
  keywds 
)
read_and_call(uhandle, method[, start][, end][, contains][, blank][, has_re])

Read a line from uhandle, check it, and pass it to the method.
Raises a ValueError if the line does not pass the checks.

start, end, contains, blank, and has_re specify optional conditions
that the line must pass.  start and end specifies what the line must
begin or end with (not counting EOL characters).  contains
specifies a substring that must be found in the line.  If blank
is a true value, then the line must be blank.  has_re should be
a regular expression object with a pattern that the line must match
somewhere.

Definition at line 292 of file ParserSupport.py.

00292 
00293 def read_and_call(uhandle, method, **keywds):
00294     """read_and_call(uhandle, method[, start][, end][, contains][, blank][, has_re])
00295 
00296     Read a line from uhandle, check it, and pass it to the method.
00297     Raises a ValueError if the line does not pass the checks.
00298 
00299     start, end, contains, blank, and has_re specify optional conditions
00300     that the line must pass.  start and end specifies what the line must
00301     begin or end with (not counting EOL characters).  contains
00302     specifies a substring that must be found in the line.  If blank
00303     is a true value, then the line must be blank.  has_re should be
00304     a regular expression object with a pattern that the line must match
00305     somewhere.
00306 
00307     """
00308     line = safe_readline(uhandle)
00309     errmsg = _fails_conditions(*(line,), **keywds)
00310     if errmsg is not None:
00311         raise ValueError(errmsg)
00312     method(line)

Here is the call graph for this function:

Here is the caller graph for this function:

def Bio.ParserSupport.read_and_call_until (   uhandle,
  method,
  keywds 
)
read_and_call_until(uhandle, method, 
start=None, end=None, contains=None, blank=None) -> number of lines

Read a line from uhandle and pass it to the method until
some condition is true.  Returns the number of lines that were read.

See the docstring for read_and_call for a description of the parameters.

Definition at line 333 of file ParserSupport.py.

00333 
00334 def read_and_call_until(uhandle, method, **keywds):
00335     """read_and_call_until(uhandle, method, 
00336     start=None, end=None, contains=None, blank=None) -> number of lines
00337 
00338     Read a line from uhandle and pass it to the method until
00339     some condition is true.  Returns the number of lines that were read.
00340 
00341     See the docstring for read_and_call for a description of the parameters.
00342     
00343     """
00344     nlines = 0
00345     while 1:
00346         line = safe_readline(uhandle)
00347         # If I've met the condition, then stop reading the line.
00348         if not _fails_conditions(*(line,), **keywds):
00349             uhandle.saveline(line)
00350             break
00351         method(line)
00352         nlines = nlines + 1
00353     return nlines

Here is the call graph for this function:

Here is the caller graph for this function:

def Bio.ParserSupport.read_and_call_while (   uhandle,
  method,
  keywds 
)
read_and_call_while(uhandle, method[, start][, end][, contains][, blank][, has_re]) -> number of lines

Read a line from uhandle and pass it to the method as long as
some condition is true.  Returns the number of lines that were read.

See the docstring for read_and_call for a description of the parameters.

Definition at line 313 of file ParserSupport.py.

00313 
00314 def read_and_call_while(uhandle, method, **keywds):
00315     """read_and_call_while(uhandle, method[, start][, end][, contains][, blank][, has_re]) -> number of lines
00316 
00317     Read a line from uhandle and pass it to the method as long as
00318     some condition is true.  Returns the number of lines that were read.
00319 
00320     See the docstring for read_and_call for a description of the parameters.
00321     
00322     """
00323     nlines = 0
00324     while 1:
00325         line = safe_readline(uhandle)
00326         # If I've failed the condition, then stop reading the line.
00327         if _fails_conditions(*(line,), **keywds):
00328             uhandle.saveline(line)
00329             break
00330         method(line)
00331         nlines = nlines + 1
00332     return nlines

Here is the call graph for this function:

Here is the caller graph for this function:

safe_peekline(handle) -> line

Peek at the next line in an UndoHandle and return it.  If there are no
more lines to peek, I will raise a ValueError.

Definition at line 424 of file ParserSupport.py.

00424 
00425 def safe_peekline(handle):
00426     """safe_peekline(handle) -> line
00427 
00428     Peek at the next line in an UndoHandle and return it.  If there are no
00429     more lines to peek, I will raise a ValueError.
00430     
00431     """
00432     line = handle.peekline()
00433     if not line:
00434         raise ValueError("Unexpected end of stream.")
00435     return line
safe_readline(handle) -> line

Read a line from an UndoHandle and return it.  If there are no more
lines to read, I will raise a ValueError.

Definition at line 412 of file ParserSupport.py.

00412 
00413 def safe_readline(handle):
00414     """safe_readline(handle) -> line
00415 
00416     Read a line from an UndoHandle and return it.  If there are no more
00417     lines to read, I will raise a ValueError.
00418 
00419     """
00420     line = handle.readline()
00421     if not line:
00422         raise ValueError("Unexpected end of stream.")
00423     return line

Here is the caller graph for this function:


Variable Documentation

Definition at line 44 of file ParserSupport.py.