Yannick Gingras | 14 May 17:06
Favicon

Pre-commit hooks


Greetings Alchemists,
  Is it possible to define a hook in a mapped class that will be called
to test the sanity of an instance before it gets committed?

As an example:

  class Item(object):
    def _pre_commit(self):
      assert (self.dry_weight + self.fluids) < 50
  mapper(Item, items_table)

I don't want to put the test mutators of dry_weight or fluids since
it's OK to have a temporary inconsistent state as long as the state is
consistent at commit time.

I see that some of this functionality if covered by MapperExtention
but since the test is only related to Item I'd rather put the test in
it.

--

-- 
Yannick Gingras

--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---

(Continue reading)

Michael Bayer | 14 May 17:25

Re: Pre-commit hooks


On May 14, 2008, at 11:07 AM, Yannick Gingras wrote:

>
>
> Greetings Alchemists,
>  Is it possible to define a hook in a mapped class that will be called
> to test the sanity of an instance before it gets committed?
>
> As an example:
>
>  class Item(object):
>    def _pre_commit(self):
>      assert (self.dry_weight + self.fluids) < 50
>  mapper(Item, items_table)
>
> I don't want to put the test mutators of dry_weight or fluids since
> it's OK to have a temporary inconsistent state as long as the state is
> consistent at commit time.
>
> I see that some of this functionality if covered by MapperExtention
> but since the test is only related to Item I'd rather put the test in
> it.
>

easy enough to build yourself a generic MapperExtension that scans  
incoming objects for a "_pre_commit()" method.

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "sqlalchemy" group.
(Continue reading)

Yannick Gingras | 15 May 15:46
Favicon

Re: Pre-commit hooks


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

    def before_delete(self, mapper, connection, instance):
        if getattr(instance, "_pre_delete", None):
            instance._pre_delete()
        return EXT_CONTINUE

    def before_update(self, mapper, connection, instance):
        if getattr(instance, "_pre_update", None):
(Continue reading)

Roger Demetrescu | 15 May 16:36
Picon

Re: Pre-commit hooks


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):
(Continue reading)

az | 15 May 16:41

Re: Pre-commit hooks


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
>
(Continue reading)

Roger Demetrescu | 15 May 17:10
Picon

Re: Pre-commit hooks


On 5/15/08, az <at> svilendobrev.com <az <at> svilendobrev.com> wrote:
>
> 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.

Good point...

Thanks,

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
-~----------~----~----~----~------~----~------~--~---

Yannick Gingras | 15 May 17:13
Favicon

Re: Pre-commit hooks


az <at> svilendobrev.com writes:

> 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.

Spelling only once is the killer feature of your approach.  I just
refactored my implementation.

Thanks!

--

-- 
Yannick Gingras

--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---

King Simon-NFHD78 | 14 May 17:29
Favicon

Re: Pre-commit hooks


Yannick Gingras wrote:
> Greetings Alchemists,
>   Is it possible to define a hook in a mapped class that will 
> be called
> to test the sanity of an instance before it gets committed?
> 
> As an example:
> 
>   class Item(object):
>     def _pre_commit(self):
>       assert (self.dry_weight + self.fluids) < 50
>   mapper(Item, items_table)
> 
> I don't want to put the test mutators of dry_weight or fluids since
> it's OK to have a temporary inconsistent state as long as the state is
> consistent at commit time.
> 
> I see that some of this functionality if covered by MapperExtention
> but since the test is only related to Item I'd rather put the test in
> it.
>  

The way I've done this in the past is to define a fairly generic
MapperExtension that calls hook methods on my classes. Here's an
example:

----------------------------------------------

import sqlalchemy as sa
(Continue reading)


Gmane