[Biopython-dev] [Bug 1716] Fasta.Dictionary should throw KeyError
for invalid keys
bugzilla-daemon at portal.open-bio.org
bugzilla-daemon at portal.open-bio.org
Sat Dec 11 08:47:46 EST 2004
http://bugzilla.open-bio.org/show_bug.cgi?id=1716
biopython-bugzilla at maubp.freeserve.co.uk changed:
What |Removed |Added
----------------------------------------------------------------------------
OS/Version|Windows 2000 |All
------- Additional Comments From biopython-bugzilla at maubp.freeserve.co.uk 2004-12-11 08:47 -------
Further investigation reveals that the __getitem__ function checks for an
invalid key by exception, and in this case attempts to "try and fetch by alias".
I'm not sure what that is meant to do, but if this also fails, I think a
KeyError should be raised rather than whatever went wrong with the alias fetch.
An except from old code in Bio/Fasta/__init.py__ from the class Dictionary:
----------------------------------------------------------------------------
def __getitem__(self, key):
# first try to retrieve by the base id
try:
seqs = self._index.lookup(id = key)
# if we can't do that, we have to try and fetch by alias
except KeyError:
seqs = self._index.lookup(aliases = key)
...
----------------------------------------------------------------------------
My suggested code:
----------------------------------------------------------------------------
def __getitem__(self, key):
# first try to retrieve by the base id
try:
seqs = self._index.lookup(id = key)
# if we can't do that, we have to try and fetch by alias
except KeyError:
try :
seqs = self._index.lookup(aliases = key)
except :
#That failed too. Rather than returning some
#cryptic ZeroDivisionError from Mindy, do this:
raise KeyError("Key (%s) not found, not even as an alias" % key)
...
----------------------------------------------------------------------------
This would appear to fix my original problem of a miss-leading error message.
The actual wording I have suggested could probably be improved...
------- 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