[Biopython-dev] [Bug 2752] New: Context management for Bio.Entrez handles

bugzilla-daemon at portal.open-bio.org bugzilla-daemon at portal.open-bio.org
Sun Feb 8 18:30:14 UTC 2009


http://bugzilla.open-bio.org/show_bug.cgi?id=2752

           Summary: Context management for Bio.Entrez handles
           Product: Biopython
           Version: 1.49
          Platform: PC
        OS/Version: Linux
            Status: NEW
          Severity: enhancement
          Priority: P2
         Component: Main Distribution
        AssignedTo: biopython-dev at biopython.org
        ReportedBy: eric.talevich at gmail.com


I'd like the following code to work:

def write_gbk(gi):
    with open("gi%s.gbk" % gi, 'w+') as outfile:
        with Entrez.efetch(db='protein, rettype='genbank', id=gi) as gbk:
            text = gbk.read()
        outfile.write(text)
    print "Wrote", gi


Since Python 2.5 it's been possible to use the "with" statement to ensure
handles are closed properly even if an exception occurs (PEP 343). There's also
a decorator, @contextlib.contextmanager, to make this feature easy to support,
but in general it works by adding the __enter__ and __exit__ methods to a
class.

To make Bio.Entrez work this way, we could just add @contextmanager decorators
to efetch() and the others, but that would break 2.3 & 2.4 compatibility, so,
it's probably best to make a factory class that returns handles on
instantiation, and includes __enter__ and __exit__ methods. The e* functions
would become trivial classes that derive from the factory; this would also make
it possible to remove the redundant code around the deprecated "cgi=None"
argument.


-- 
Configure bugmail: http://bugzilla.open-bio.org/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are the assignee for the bug, or are watching the assignee.



More information about the Biopython-dev mailing list