>>> from collections import namedtuple
>>> LightObject = namedtuple('LightObject', ['shortname', 'otherprop'])
>>> m = LightObject()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: __new__() takes exactly 3 arguments (1 given)
>>> m = LightObject("first", "second")
>>> m.shortname
'first'
>>> m.shortname = 'athing'
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: can't set attribute
Also, bare try/excepts as in: try:
# get API data
data = db.find(id='foo') # may raise exception
except:
# log the failure and bail out
log.warn("Could not retrieve FOO")
return
are really bad. The failure might be caused by a ^C or MemoryError, or even a SystemExit, should db.find() desire to do that.Instead, qualify it by catching Exception:
try:
# get API data
data = db.find(id='foo') # may raise exception
except Exception:
# log the failure and bail out
log.warn("Could not retrieve FOO")
return
It's also poor form to "return True" in the exit method of the context manager. If there is no exception then that's not needed at all, and if there is an exception ... well, that code will swallow AttributeError and NameError and ZeroDivisionError, and leave people confused as to the source of the error.