[Grok-dev] Re: Grok and database generations
Leonardo Rochael Almeida
leorochael at gmail.com
Wed Sep 5 08:15:13 EDT 2007
Hi Sebastian,
On 9/5/07, Sebastian Ware <sebastian at urbantalk.se> wrote:
> Ok. I understand there are some pitfalls here. I have opted to add a
> "update_schema" method to my objects...
>
> def update_schema(self):
> try:
> if self.render_type is None:
> self.render_type = 'table'
> except:
> self.render_type = 'table'
hmm, bad use of a generic except here. The ZODB itself could raise a
ConflictError in your code that should be handled by the request
machinery and you would be intercepting that, possibly leaving your
database in an inconsistent state (don't know if it's still a problem
like it used to be a few years ago or if the transaction would be
"doomed" anyway).
The case above is a perfect example of where a class attribute would
be useful. 'table' is a string, which is immutable, so there would be
no danger of changing a shared attribute between instances. If you
were instead setting a mutable, like, say, a PersistentList, you
probably want to do something like:
> def update_schema(self):
> if getattr(self, 'my_list', None) is None:
> self.my_list = PersistentList()
or, what I consider cleaner:
> my_list = None
> def update_schema(self):
> if self.my_list is None:
> self.my_list = PersistentList()
And if you really want to do an "except:", do it with the specific
exception you're expecting, ("except AttributeError:" in your case).
Cheers, Leo
More information about the Grok-dev
mailing list