15 May 16:41
Re: Pre-commit hooks
From: <az <at> svilendobrev.com>
Subject: Re: Pre-commit hooks
Newsgroups: gmane.comp.python.sqlalchemy.user
Date: 2008-05-15 14:41:40 GMT
Subject: Re: Pre-commit hooks
Newsgroups: gmane.comp.python.sqlalchemy.user
Date: 2008-05-15 14:41:40 GMT
speed wise, this is better: hasattr is implemented as getattr + try except. i would do it even: f = getattr(instance, "_pre_insert", None) if f: f() Thus the func name is spelled only once - avoids stupid mistakes. On Thursday 15 May 2008 17:36:52 Roger Demetrescu wrote: > On 5/15/08, Yannick Gingras <ygingras <at> ygingras.net> wrote: > > Michael Bayer <mike_mp <at> zzzcomputing.com> writes: > > > easy enough to build yourself a generic MapperExtension that > > > scans incoming objects for a "_pre_commit()" method. > > > > Yeah indeed. I used this: > > > > ------------------------------ > > class HookExtension(MapperExtension): > > """ Extention to add pre-commit hooks. > > > > Hooks will be called in Mapped classes if they define any of > > these methods: > > * _pre_insert() > > * _pre_delete() > > * _pre_update() > > """ > > def before_insert(self, mapper, connection, instance): > > if getattr(instance, "_pre_insert", None): > > instance._pre_insert() > > return EXT_CONTINUE > > [cut] > > Any reason for not using hasattr ? Like... > > def before_insert(self, mapper, connection, instance): > if hasattr(instance, "_pre_insert"): > instance._pre_insert() > return EXT_CONTINUE > > > Cheers, > > Roger > > --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "sqlalchemy" group. To post to this group, send email to sqlalchemy <at> googlegroups.com To unsubscribe from this group, send email to sqlalchemy-unsubscribe <at> googlegroups.com For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en -~----------~----~----~----~------~----~------~--~---
RSS Feed