Error type for shelve.open()
I wanted to write the following code: import shelve try: db = shelve.open(file, "r") except SomeError: print "Oh no, db not found" Only, I'm not sure what SomeError should be. I tried error, anydbm.error, shelve.open.anydb.error, etc. but can't find it. Things worked fine with simply except:, but that's not optimal. Does anyone know either the what the error is or where I can find it for certain? Thanks, Martin -- http://mail.python.org/mailman/listinfo/python-list
Re: Error type for shelve.open()
I tried what you said and it looked like maybe AttributeError, but that didn't work either. This code snippet: import shelve from traceback import format_exc try: db = shelve.open("meh", "r") except: print format_exc() Gave me this output: Traceback (most recent call last): File "test.py", line 5, in ? db = shelve.open("meh", "r") File "/usr/lib/python2.4/shelve.py", line 231, in open return DbfilenameShelf(filename, flag, protocol, writeback, binary) File "/usr/lib/python2.4/shelve.py", line 212, in __init__ Shelf.__init__(self, anydbm.open(filename, flag), protocol, writeback, binary) File "/usr/lib/python2.4/anydbm.py", line 77, in open raise error, "need 'c' or 'n' flag to open new db" error: need 'c' or 'n' flag to open new db Exception exceptions.AttributeError: "DbfilenameShelf instance has no attribute 'writeback'" in ignored Do you know what the error is? Thanks, Martin Simon Forman wrote: > [EMAIL PROTECTED] wrote: > > I wanted to write the following code: > > > > import shelve > > try: > >db = shelve.open(file, "r") > > except SomeError: > >print "Oh no, db not found" > > > > Only, I'm not sure what SomeError should be. I tried error, > > anydbm.error, shelve.open.anydb.error, etc. but can't find it. Things > > worked fine with simply except:, but that's not optimal. > > > > Does anyone know either the what the error is or where I can find it > > for certain? > > > > Thanks, > > Martin > > What error did you get with just the bare except? > > you can find out by saying (for instance): > > >>> try: > 1/0 > except Exception, err: > E = err > > > >>> E > > > > or to get really fancy, use the traceback module > > >>> from traceback import format_exc > >>> try: > 1/0 > except: > print format_exc() > > > Traceback (most recent call last): > File "", line 2, in ? > ZeroDivisionError: integer division or modulo by zero > > > > Peace, > ~Simon -- http://mail.python.org/mailman/listinfo/python-list
Re: Error type for shelve.open()
Yes, the problem was that I hadn't imported anydbm.error... it's working now. As for the AttributeError at the end, I talked to someone else, and he looked at the source and said it was a bug in shelve. I think I will report it to python.org. Anyway, thanks :). Simon Forman wrote: > [EMAIL PROTECTED] wrote: > > I tried what you said and it looked like maybe AttributeError, but that > > didn't work either. > > > > This code snippet: > > > > import shelve > > from traceback import format_exc > > > > try: > >db = shelve.open("meh", "r") > > except: > >print format_exc() > > > > Gave me this output: > > Traceback (most recent call last): > > File "test.py", line 5, in ? > > db = shelve.open("meh", "r") > > File "/usr/lib/python2.4/shelve.py", line 231, in open > > return DbfilenameShelf(filename, flag, protocol, writeback, binary) > > File "/usr/lib/python2.4/shelve.py", line 212, in __init__ > > Shelf.__init__(self, anydbm.open(filename, flag), protocol, > > writeback, binary) > > File "/usr/lib/python2.4/anydbm.py", line 77, in open > > raise error, "need 'c' or 'n' flag to open new db" > > error: need 'c' or 'n' flag to open new db > > > > Exception exceptions.AttributeError: "DbfilenameShelf instance has no > > attribute 'writeback'" in ignored > > > > Do you know what the error is? > > No. If you tried catching AttributeError and it didn't work then I'd > guess that the AttributeError is a secondary result of the initial > error. > > This part of the traceback, > > > File "/usr/lib/python2.4/anydbm.py", line 77, in open > > raise error, "need 'c' or 'n' flag to open new db" > > error: need 'c' or 'n' flag to open new db > > indicates that some sort of custom error, probably defined in the > anydbm.py module. > > Catching the execption and binding it to a var, > > >>> try: > ... db = shelve.open("meh", "r") > ... except Exception, err: > ... E = err > ... > Exception exceptions.AttributeError: "DbfilenameShelf instance has no > attribute 'writeback'" in ignored > >>> E > > > > > So: > >>> from anydbm import error > >>> try: > ... db = shelve.open("meh", "r") > ... except error: > ... print 'Aha! got it!' > ... > Aha! got it! > Exception exceptions.AttributeError: "DbfilenameShelf instance has no > attribute 'writeback'" in ignored > > > Well, that catches the error, but I don't know what's going on with the > additional AttributeError or what to do about it. > > Peace, > ~Simon -- http://mail.python.org/mailman/listinfo/python-list
Re: Error type for shelve.open()
I reported the bug to python.org and apparently it has already been fixed in the latest SVN build :). [EMAIL PROTECTED] wrote: > Yes, the problem was that I hadn't imported anydbm.error... it's > working now. > > As for the AttributeError at the end, I talked to someone else, and he > looked at the source and said it was a bug in shelve. I think I will > report it to python.org. > > Anyway, thanks :). > Simon Forman wrote: > > [EMAIL PROTECTED] wrote: > > > I tried what you said and it looked like maybe AttributeError, but that > > > didn't work either. > > > > > > This code snippet: > > > > > > import shelve > > > from traceback import format_exc > > > > > > try: > > >db = shelve.open("meh", "r") > > > except: > > >print format_exc() > > > > > > Gave me this output: > > > Traceback (most recent call last): > > > File "test.py", line 5, in ? > > > db = shelve.open("meh", "r") > > > File "/usr/lib/python2.4/shelve.py", line 231, in open > > > return DbfilenameShelf(filename, flag, protocol, writeback, binary) > > > File "/usr/lib/python2.4/shelve.py", line 212, in __init__ > > > Shelf.__init__(self, anydbm.open(filename, flag), protocol, > > > writeback, binary) > > > File "/usr/lib/python2.4/anydbm.py", line 77, in open > > > raise error, "need 'c' or 'n' flag to open new db" > > > error: need 'c' or 'n' flag to open new db > > > > > > Exception exceptions.AttributeError: "DbfilenameShelf instance has no > > > attribute 'writeback'" in ignored > > > > > > Do you know what the error is? > > > > No. If you tried catching AttributeError and it didn't work then I'd > > guess that the AttributeError is a secondary result of the initial > > error. > > > > This part of the traceback, > > > > > File "/usr/lib/python2.4/anydbm.py", line 77, in open > > > raise error, "need 'c' or 'n' flag to open new db" > > > error: need 'c' or 'n' flag to open new db > > > > indicates that some sort of custom error, probably defined in the > > anydbm.py module. > > > > Catching the execption and binding it to a var, > > > > >>> try: > > ... db = shelve.open("meh", "r") > > ... except Exception, err: > > ... E = err > > ... > > Exception exceptions.AttributeError: "DbfilenameShelf instance has no > > attribute 'writeback'" in ignored > > >>> E > > > > > > > > > > So: > > >>> from anydbm import error > > >>> try: > > ... db = shelve.open("meh", "r") > > ... except error: > > ... print 'Aha! got it!' > > ... > > Aha! got it! > > Exception exceptions.AttributeError: "DbfilenameShelf instance has no > > attribute 'writeback'" in ignored > > > > > > Well, that catches the error, but I don't know what's going on with the > > additional AttributeError or what to do about it. > > > > Peace, > > ~Simon -- http://mail.python.org/mailman/listinfo/python-list
Speed up this code?
I'm creating a program to calculate all primes numbers in a range of 0 to n, where n is whatever the user wants it to be. I've worked out the algorithm and it works perfectly and is pretty fast, but the one thing seriously slowing down the program is the following code: def rmlist(original, deletions): return [i for i in original if i not in deletions] original will be a list of odd numbers and deletions will be numbers that are not prime, thus this code will return all items in original that are not in deletions. For n > 100,000 or so, the program takes a very long time to run, whereas it's fine for numbers up to 10,000. Does anybody know a faster way to do this? (finding the difference all items in list a that are not in list b)? Thanks, Martin -- http://mail.python.org/mailman/listinfo/python-list
Re: Speed up this code?
I got it working using difference() and sets, thanks all! 100,000 takes about 3 times the time of 10,000, which is what my math buddies told me I should be getting, rather than an exponential increase :). Thanks, all! -- http://mail.python.org/mailman/listinfo/python-list