[Biopython-dev] [RFC, PATCH] Bio.Iter

Jeffrey Chang jchang at jeffchang.com
Wed Sep 10 14:24:47 EDT 2003


Hi Yves,

Does this not do the same thing?
   b_iterator = iter(NCBIStandalone.Iterator(blast_out, b_parser).next, 
None)

It's a little uglier than your solution.  However, the best would be to 
make NCBIStandalone.Iterator a true iterator.  Could you also try 
adding, to the NCBIStandalone.Iterator class the method:

   def __iter__(self):
     return iter(self.next, None)

I have not tested it.  However, if it works correctly for you, please 
let me know and I can add it to the distribution.

Jeff



On Wednesday, September 10, 2003, at 09:35  AM, Yves Bastide wrote:

> Hi all,
>
> here's a small wrapper for using Bio iterators the 'modern' way.
> E. g., replacing:
>
> from Bio.Blast import NCBIStandalone
>
> blast_out = open('my_file_of_blast_output', 'r')
> b_parser = NCBIStandalone.BlastParser()
> b_iterator = NCBIStandalone.Iterator(blast_out, b_parser)
>
> while 1:
>     b_record = b_iterator.next()
>
>     if b_record is None:
>         break
>     # ...
>
>
> With:
>
> from Bio.Blast import NCBIStandalone
> from Bio.Iter import Iter
>
> blast_out = file('my_file_of_blast_output', 'r')
> b_parser = NCBIStandalone.BlastParser()
> b_iterator = Iter(NCBIStandalone.Iterator(blast_out, b_parser))
>
> for b_record in b_iter:
>     # ...
>
>
> Regards,
>
> yves
> Index: biopython/Bio/__init__.py
> ===================================================================
> RCS file: /home/repository/biopython/biopython/Bio/__init__.py,v
> retrieving revision 1.21
> diff -u -p -r1.21 __init__.py
> --- biopython/Bio/__init__.py	2003/02/20 04:35:37	1.21
> +++ biopython/Bio/__init__.py	2003/09/10 16:25:44
> @@ -18,6 +18,7 @@ __all__ = [
>      "Gobase",
>      "Index",
>      "InterPro",
> +    "Iter",
>      "KEGG",
>      "Kabat",
>      "Medline",
> --- /dev/null	2003-01-30 11:24:37.000000000 +0100
> +++ biopython/Bio/Iter.py	2003-09-10 18:22:41.000000000 +0200
> @@ -0,0 +1,12 @@
> +# This code is part of the Biopython distribution and governed by its
> +# license.  Please see the LICENSE file that should have been included
> +# as part of this package.
> +from __future__ import generators
> +
> +def Iter(bio_iter):
> +    """Standard iterator wrapper for Bio iterators"""
> +    while True:
> +        ret = bio_iter.next()
> +        if ret is None:
> +            return
> +        yield ret
> _______________________________________________
> Biopython-dev mailing list
> Biopython-dev at biopython.org
> http://biopython.org/mailman/listinfo/biopython-dev




More information about the Biopython-dev mailing list